jeudi 5 septembre 2019

What is a better way to write an if statment to avoid some list items?

I have a list where I know that some items are unnecessary to print and I'm trying to do that through if statement... but it's getting very convoluted so is there any way to include multiple indexes in the if statement without rewriting whole statement. Something that would look like like this?: if x == chart[0,2,4]

Example that I have:

chart = ['milk', 'soda', 'cookies', 'yogurt', 'rug']

for x in chart:
    if x == chart[0] or x == chart[2] or x == chart[4]:
        continue
    else:
        print(x)

Aucun commentaire:

Enregistrer un commentaire