Apparently
METHOD 1
if var in ['string one', 'string two']:
do_something()
is more Pythonic than:
METHOD 2
if var == 'stringone' or var == 'stringtwo':
dosomething()
Why is Method 1 considered more Pythonic than Method 2?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
From: What is Pythonic?
Simply put, the first is easier to read than the second – it has less boilerplate, and less overhead than the first. Any Python programmer can look at the first and see that there’s a list of things being checked against, and it reads much more like plain English than the second. Consider if you expanded the list of things being checked against – the first example would read like:
while the second would sound like: