Skip to content
Advertisement

How to iterate through list and search for several lists

These are the grocery store lists:

JavaScript

What I have to do is take input for what a customer wants to buy and I have done that through this loop:

JavaScript

I also already have sent empty lists for the grocery_list and have a list called Grocery_items that just combines each list above with +. Lastly, I set another empty list for each aisle. Ex. a1 = [] a2 = [].

My issue is that I have to make one FOR loop that iterates through the grocery list and the loop has to add each item in the list to the empty variable for each aisle. For example, aisle_1 contains honey so if the grocery_list has honey, it should be added to the empty list a1.

This is what i have come up with:

JavaScript

For some reason, this whole thing is just ignored and when i print one of the lists that should be changed, it just shows empty. For example, the use inputs honey and grits, in the For loop it should go through the list and see that honey is in aisle one and add that to a1 then see that grits is also in aisle one and adds that to a1. I do not know why it is not adding it to each list.

Final code I have at the moment:

JavaScript

at the end if I add print a1, instead of printing the grocery items that should be in aisle one it just prints a blank. ex. []

Advertisement

Answer

Make sure you are using item.lower() and not item.lower. I would also use a dictionary, where the key is the name of the aisle, and the value is a list of items in that aisle.

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement