logo

... comprehensions.


Python has a system that might simplify your code called comprehensions. They allow you to turn your nested for loop into an amazing single line of code. In this series of videos we'll highlight some useful ways to use them.


Notes

Understanding how python does unpacking allows you to create many new variants of comprehensions.

arr = [('a', 1), ('b', 2), ('c', 2)]
for idx, (char, i) in enumerate(arr):
    print(idx, char, i)

Understanding this unpacking allows for very expressive one-liners.

[{key: value, 'i': idx} for idx, (key, value) in enumerate(arr)]

Feedback? See an issue? Something unclear? Feel free to mention it here.

If you want to be kept up to date, consider getting the newsletter.