Double Iteration in List Comprehension

Here’s something I didn’t know possible in Python: iteration over more than one iterable in a list comprehension:

This is a companion discussion topic for the original entry at

Somewhat related to list comprehensions: A few months back I found out you can write multiple ifs one after the other and they'll be interpreted as if there was an and between them:

[i for i in xrange(10) if i > 3 if i < 6]

It's probably an anti feature as less than 10 people in the world know about it's existance.

Holy shit Alon, you blew my mind :)

I completely agree it's an anti-feature, but it's interesting to know that the syntax is allowed.


I actually used this a few days ago. I wanted to see if it'll pass our code review. It did, not questions asked :)