python - Write conditions based on a list -


i'm writing if statement in python lot of or conditions. there elegant way list, within in condition rather looping through list?

in other words, prettier following:

if foo == 'a' or foo == 'b' or foo == 'c' or foo == 'd': 

i've taken python, , language has me wanting write better.

if foo in ('a', 'b', 'c', 'd'):     #... 

i note answer wrong several reasons:

  1. you should remove parentheses.. python need outer ones , takes room.
  2. you're using assignment operator, not equality operator (=, not ==)
  3. what meant write foo == 'a' or foo == 'b' or ..., wrote wasn't quite correct.

Comments

Popular posts from this blog

ruby - When to use an ORM (Sequel, Datamapper, AR, etc.) vs. pure SQL for querying -

php - PHPDoc: @return void necessary? -

c++ - Convert big endian to little endian when reading from a binary file -