Do you want to master Python ๐ list comprehensions?
Do you want to become more proficient working with lists in Python ๐?
If you answered with โyesโ, then here's a cheatsheet of mine to help you out.
This is also a thread ๐๐งต that breaks it down and explains everything:
First off, here is the anatomy of a list comprehension.
There's 4 parts to a list comp:
1. enclosing brackets to create the list [];
2. expression that transforms your data;
3. `for` iteration over the initial data;
4. (optional!) condition to filter some data.
Do list comprehensions look confusing?
Maybe.
But that's because you didn't realise that list comprehensions have equivalent `for` loops!
The coloured boxes below show the equivalent bits in the list comp and in the `for` loop: