Python Forum
Iterating through lists with different letter cases - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Iterating through lists with different letter cases (/thread-27451.html)



Iterating through lists with different letter cases - fatherted99 - Jun-07-2020

menu = []
menu.append(["egg", "spam", "bacon"])
menu.append(["egg", "sausage", "bacon"])
menu.append(["egg", "spam"])
menu.append(["egg", "bacon", "spam"])
menu.append(["egg", "bacon", "sausage", "spam"])
menu.append(["spam", "bacon", "sausage", "spam"]) # should only print out this one
menu.append(["spam", "Egg", "spam", "spam", "bacon", "spam"])
menu.append(["spam", "Egg", "sausage", "spam"])

# print(menu)

for meal in menu:
    for ingredient in meal:
        if ingredient.lower() != "egg":
            print(meal)
            
        
I want to only print out the meal which does not contain "egg". In the menu variable, I have purposely appended lists which contain an uppercase "Egg". Therefore when I go through each meal in menu, and each ingredient in meal, I will convert ingredient to lower case so that it does not matter if the ingredient is "Egg" or "egg" in a meal.

Output:

['egg', 'spam', 'bacon']
['egg', 'spam', 'bacon']
['egg', 'sausage', 'bacon']
['egg', 'sausage', 'bacon']
['egg', 'spam']
['egg', 'bacon', 'spam']
['egg', 'bacon', 'spam']
['egg', 'bacon', 'sausage', 'spam']
['egg', 'bacon', 'sausage', 'spam']
['egg', 'bacon', 'sausage', 'spam']
['spam', 'bacon', 'sausage', 'spam']
['spam', 'bacon', 'sausage', 'spam']
['spam', 'bacon', 'sausage', 'spam']
['spam', 'bacon', 'sausage', 'spam']
['spam', 'Egg', 'spam', 'spam', 'bacon', 'spam']
['spam', 'Egg', 'spam', 'spam', 'bacon', 'spam']
['spam', 'Egg', 'spam', 'spam', 'bacon', 'spam']
['spam', 'Egg', 'spam', 'spam', 'bacon', 'spam']
['spam', 'Egg', 'spam', 'spam', 'bacon', 'spam']
['spam', 'Egg', 'sausage', 'spam']
['spam', 'Egg', 'sausage', 'spam']
['spam', 'Egg', 'sausage', 'spam']



It should only print out the meal: ['spam', 'bacon', 'sausage', 'spam']
But as you can see this is not the case
Any reason why?

Thanks in advance,
Python beginner


RE: Iterating through lists with different letter cases - DPaul - Jun-07-2020

You are testing each ingredient for nog being 'egg',
'spam' is not 'egg' so it will print.
Maybe this will do:
for meal in menu:
    meal = [ingredient.lower() for ingredient in meal]
    if not 'egg' in meal:
       print(meal)
Paul


RE: Iterating through lists with different letter cases - deanhystad - Jun-07-2020

Your logic prints the list whenever an ingredient is not "egg". The only meal it would not print would be "egg" (with any combination of upper and lower case letters.

You need to test every ingredient in the meal and only print the meal if none of the ingredients are egg.
for meal in menu:
    for ingredient in meal:
        if ingredient.lower() == "egg":
             break
    else:
        print(meal)