mardi 27 décembre 2016

Building an IF statement with an Array - Python [duplicate]

This question already has an answer here:

Pets = ["Dog", "Cat", "Hamster"]

I want to achieve this if statement:

if Pet == "Dog" or Pet == "Cat" or Pet == "Hamster":
    print "Correct!"

But my array has over 100+ items, so is there a quicker way to iterate over the array for an if statement like this instead of typing them all out? I know I can do:

for name in Pets:
   if Pet == name:
       print "Correct!"

But surely this is a lot slower as it's doing multiple if statements instead of just 1 longer one?

Thanks for looking/helping :)

Aucun commentaire:

Enregistrer un commentaire