This post is a few years old now, so some details (or my opinions) might be out of date. I would still love to hear your feedback in the comments below. Enjoy!
Here’s something I didn’t know possible in Python: iteration over more than one iterable in a list comprehension:
Cool, isn’t it? It’s equivalent to the following snippet:
It also supports both “if” statements and referencing the outer iterator from the inner one, like so:
This is equivalent to the snippet:
The thing you should notice here, is that the outer loop is the first ‘for’ loop in the list comprehension. This was a little confusing for me at first because when I nest list comprehensions it’s the other way around.