Python Forum
Iterating through lists with different letter cases
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Iterating through lists with different letter cases
#1
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
Reply
#2
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
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#3
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Uses cases for list comprehension (encountered while doing PyBite #77) Drone4four 3 2,739 Sep-25-2020, 12:14 PM
Last Post: ndc85430
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,312 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  Decorator staticmethod Use Cases Devarishi 3 2,600 May-20-2019, 04:27 AM
Last Post: Devarishi
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 3,190 Mar-20-2019, 08:01 PM
Last Post: stillsen
  Iterating over two lists in python dgm 2 2,975 Jul-24-2018, 08:31 PM
Last Post: dgm
  Why does my Switch trigger all Cases? IAMK 6 3,834 Apr-09-2018, 11:12 AM
Last Post: snippsat
  Best form of iterating over a lsit of lists tonymcgregor 15 7,564 Oct-15-2017, 11:19 PM
Last Post: tonymcgregor
  iterating over N lists in parallel Skaperen 6 5,114 Mar-24-2017, 06:51 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020