Skip to content
Advertisement

How to iterate through list and search for several lists

These are the grocery store lists:

aisle_1 = ['cereals', 'cocoa', 'grits', 'honey', 'oats', 'syrup', 'tea']

aisle_2 = ['apple sauce','canned fruits', 'dried fruits', 'canned juices', 'canned meats', 'prunes', 'raisins', 'canned seafood', 'soup' ]

aisle_3 = ['baking soda', 'cake mixes', 'extracts', 'flour', 'gelatins', 'ketchup', 'mayonnaise', 'meal', 'mustard', 'oils', 'olives', 'pickles','pie crust','salad dressing', 'salt', 'spices', 'sugar', 'vinegar', 'yeast' ]

aisle_4 = ['international foods', 'macaroni', 'canned mushrooms', 'rice', 'meat sauces', 'dried spaghetti', 'tomato paste', 'canned vegetables']

aisle_5 = ['cookies', 'crackers','nuts', 'popcorn', 'potato chips']

aisle_6 = ['matches', 'toilet paper', 'tissue', 'soft drinks', 'toothpicks'] 

aisle_7 = ['ammonia', 'bleaches', 'brooms', 'mops', 'candles', 'disinfectants', 'insectisides', 'moth balls', 'paper towels','napkins','paper plates', 'soap-detergents', 'waxes'] 

aisle_8 = ['dog food', 'light bulbs', 'pet foods', 'toys/games']

aisle_9 = ['baby needs', 'bar soap', 'batteries']

aisle_10 = ['frozen meats', 'frozen meals', 'water'] 

aisle_11 = ['frozen breakfast', 'frozen bread'] 

aisle_12 = ['beer', 'ice cream', 'frozen yogurt', 'popsicles', 'ice'] 

aisle_13 = ['bread', 'candy', 'dairy products', 'jellies', 'margarine', 'marshmallows', 'peanut butter']

rear_left= ['eggs', 'milk', 'yogurt', 'sour cream']

rear_right = ['gluten free']

produce = ['vegetables']

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

while True:
  shoppinglist = input('enter a grocery item you need:')
  if shoppinglist.lower() == 'quit':
      break
  if shoppinglist.lower() in grocery_items:
      grocery_list.append(shoppinglist) 
  else:
    print('that item is unavailable')

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:

for item in grocery_list:
  if item.lower in aisle_1:
    a1.append(item)
  if item.lower in aisle_2:
    a2.append(item)
  if item.lower in aisle_3:
    a3.append(item)
  if item.lower in aisle_4:
    a4.append(item)
  if item.lower in aisle_5:
    a5.append(item)
  if item.lower in aisle_6:
    a6.append(item)
  if item.lower in aisle_7:
    a7.append(item)
  if item.lower in aisle_8:
    a8.append(item)
  if item.lower in aisle_9:
    a9.append(item)
  if item.lower in aisle_10:
    a10.append(item)
  if item.lower in aisle_11:
    a11.append(item)
  if item.lower in aisle_12:
    a12.append(item)
  if item.lower in aisle_13:
    a13.append(item)
  if item.lower in rear_left:
    rear_l.append(item)
  if item.lower in rear_right:
    rear_r.append(item)
  if item.lower in produce:
    prod.append(item)

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:

aisle_1 = ['cereals', 'cocoa', 'grits', 'honey', 'oats', 'syrup', 'tea']

aisle_2 = ['apple sauce','canned fruits', 'dried fruits', 'canned juices', 'canned meats', 'prunes', 'raisins', 'canned seafood', 'soup' ]

aisle_3 = ['baking soda', 'cake mixes', 'extracts', 'flour', 'gelatins', 'ketchup', 'mayonnaise', 'meal', 'mustard', 'oils', 'olives', 'pickles','pie crust','salad dressing', 'salt', 'spices', 'sugar', 'vinegar', 'yeast' ]

aisle_4 = ['international foods', 'macaroni', 'canned mushrooms', 'rice', 'meat sauces', 'dried spaghetti', 'tomato paste', 'canned vegetables']

aisle_5 = ['cookies', 'crackers','nuts', 'popcorn', 'potato chips']

aisle_6 = ['matches', 'toilet paper', 'tissue', 'soft drinks', 'toothpicks'] 

aisle_7 = ['ammonia', 'bleaches', 'brooms', 'mops', 'candles', 'disinfectants', 'insectisides', 'moth balls', 'paper towels','napkins','paper plates', 'soap-detergents', 'waxes'] 

aisle_8 = ['dog food', 'light bulbs', 'pet foods', 'toys/games']

aisle_9 = ['baby needs', 'bar soap', 'batteries']

aisle_10 = ['frozen meats', 'frozen meals', 'water'] 

aisle_11 = ['frozen breakfast', 'frozen bread'] 

aisle_12 = ['beer', 'ice cream', 'frozen yogurt', 'popsicles', 'ice'] 

aisle_13 = ['bread', 'candy', 'dairy products', 'jellies', 'margarine', 'marshmallows', 'peanut butter']

rear_left= ['eggs', 'milk', 'yogurt', 'sour cream']

rear_right = ['gluten free']

produce = ['vegetables']
#Define the empty lists or variables you may need.
#You will need an empty list for the user inputed grocery list
#Also a blank list for each aisle that corresponds to the aisles above. This is where your grocery list items will be separated by aisle.
#You will also need a list that combines the items from the aisles above into one big list. 
grocery_list = []

a1 = []
a2 = []
a3 = []
a4 = []
a5 = []
a6 = []
a7 = []
a8 = []
a9 = []
a10 = []
a11 = []
a12 = []
a13= []
rear_r = []
rear_l = []
prod = []

grocery_items = aisle_1 + aisle_2 + aisle_3 + aisle_4 + aisle_5 + aisle_6 + aisle_7 + aisle_8 + aisle_9 + aisle_10 + aisle_11 + aisle_12 + aisle_13 + rear_right + rear_left + produce

#Construct a while loop that takes user input
#If the user enters "quit" stop the loop
#If the grocery item is available in the store, add it to your grocery list 
#If a item is not available in the store, print a message stating it is unavailable

while True:
  shoppinglist = input('enter a grocery item you need:')
  if shoppinglist.lower() == 'quit':
      break
  if shoppinglist.lower() in grocery_items:
      grocery_list.append(shoppinglist) 
  else:
    print('that item is unavailable')

#Print your entire grocery list
print('This is your grocery list: ', grocery_list)

#Construct a for loop that iterates through the grocery list
#Each item in the grocery list should be added to a blank list that corresponds to an aisle from the store
for item in grocery_list:
  if item.lower in aisle_1:
    a1.append(item)
  if item.lower in aisle_2:
    a2.append(item)
  if item.lower in aisle_3:
    a3.append(item)
  if item.lower in aisle_4:
    a4.append(item)
  if item.lower in aisle_5:
    a5.append(item)
  if item.lower in aisle_6:
    a6.append(item)
  if item.lower in aisle_7:
    a7.append(item)
  if item.lower in aisle_8:
    a8.append(item)
  if item.lower in aisle_9:
    a9.append(item)
  if item.lower in aisle_10:
    a10.append(item)
  if item.lower in aisle_11:
    a11.append(item)
  if item.lower in aisle_12:
    a12.append(item)
  if item.lower in aisle_13:
    a13.append(item)
  if item.lower in rear_left:
    rear_l.append(item)
  if item.lower in rear_right:
    rear_r.append(item)
  if item.lower in produce:
    prod.append(item)
    
#Print the aisle names and the grocery list items on each aisle
#dont worry about this part ik how to do this

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.

inventory = dict()
inventory['aisle_1'] = ['cereals', 'cocoa', 'grits', 'honey', 'oats', 'syrup', 'tea']

inventory['aisle_2'] = ['apple sauce','canned fruits', 'dried fruits', 'canned juices', 'canned meats', 'prunes', 'raisins', 'canned seafood', 'soup' ]

inventory['aisle_3'] = ['baking soda', 'cake mixes', 'extracts', 'flour', 'gelatins', 'ketchup', 'mayonnaise', 'meal', 'mustard', 'oils', 'olives', 'pickles','pie crust','salad dressing', 'salt', 'spices', 'sugar', 'vinegar', 'yeast' ]

inventory['aisle_4'] = ['international foods', 'macaroni', 'canned mushrooms', 'rice', 'meat sauces', 'dried spaghetti', 'tomato paste', 'canned vegetables']

...

inventory['rear_left'] = ['eggs', 'milk', 'yogurt', 'sour cream']

inventory['rear_right'] = ['gluten free']

inventory['produce'] = ['vegetables']

shopping_list = {key: [] for key in inventory.keys()}

#Construct a while loop that takes user input

while True:
  item = input('enter a grocery item you need:')
  if item.lower() == 'quit':
      break
  in_store = False
  for aisle, items in inventory.items():
      if item.lower() in items:
          shopping_list[aisle].append(item)
          in_store = True
          break
  if not in_store:
      print('item not available')
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement