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:
Here is an example list comprehension building a series of square numbers.
Try to work out the equivalent `for` loop and study this example until it makes sense!
Bonus points if you realise the previous list comprehension, using the condition to filter, is equivalent to the better
```py
[n ** 2 for n in range(0, 10, 2)]
```
But that beside the point, here.
Here's some more examples...
Let's take a sentence and split it into words.
Now, let's upper case each of the words.
Then, let's upper case each word, but only if it was all lower case before.
Again, study this example and write out the nested version of this is needed.
Here's one final example, for now.
Again, we break a sentence into words, and then figure out the length of each of the words.
Then, we do the same thing, but only figure out the length of words that do not have any punctuation whatsoever.
List comprehensions are already showing how powerful and useful they can be π₯!
But list comprehensions can get even more powerful than this!
Did you know you can nest `for` loops?
Useful when you have nested data.
But the nested `for` loops can be independent of one another.
This, in turn, is useful if you need to draw data from multiple iterables to combine them.
This type of situation is also often dealt with by using the built-in function `zip`.
Similarly, you can nest several `if` statements to create more complex filters.
The `if`s to the right only get triggered if the `if`s to the left evaluate to `True`.
This is the same as combining the conditions with an `and`, because of Boolean short-circuiting.
This shows that `for` loops can be nested/chained, and so can `if` statements.
This is, probably, the thing that confuses most people: the order in which things need to be written.
The very first thing in the list comprehension is the final, possibly transformed, value...
Then, from left to right, come all the nested `for` and `if` statements, from top to bottom.
This shows that it is a bad idea to nest too many things in a list comprehension.
That's because it creates a large distance between name usage and name definition.
This thread is coming to an end, but there's still a couple of things I'd like to say.
There's two other Pythonic use cases for list comprehensions.
One of those, is to replace usages of `map`:
The other use-case, a similar one, is to replace usages of `filter`:
β Β But there's something worth mentioning.
I'm not saying that `map` and `filter` are useless.
There _are_ use cases for those two built-ins.
If you want to learn about those, you can follow @mathsppblog, I'll write about it in the future.
This was a breakdown of the cheatsheet you saw in the beginning, which you can get for free π
The cheatsheet, in turn, was me summarising my latest blog article, where I introduce list comprehensions: π mathspp.com/blog/pydonts/lβ¦
If you want to level up your Python π game, and if you are a curious person, follow @mathsppblog for more great content!
Also, if you found value in this thread, retweet it for others to learn something new today π
Here's a TL;DR of this thread:
1. list comps have 4 parts, 1 of which is optional; 2. list comps have equivalent `for` loops that are helpful to understand the list comp; 3. you can nest arbitrarily many `for`s; 4. the nested `for` loops can be independent of each other;
5. you can nest arbitrarily many `if`s; 6. nesting `if`s is equivalent to combining conditions with the `and` operator; 7. all these things can be mixed and nested; 8. nesting too much is a bad idea; 9. `map` and `filter`-like behaviour can be implemented with list comps.
β’ β’ β’
Missing some Tweet in this thread? You can try to
force a refresh
If you type `import this` in a Python REPL, you will be presented with the Zen of Python, a document with some guidelines that you can follow when writing code.