Comprehensions in Python

Photo by Tomas Sobek on Unsplash

Comprehensions in Python

Do you work with Python comprehensions in your day-to-day?

Comprehensions are a straightforward way to create lists, sets, dictionaries and generators from existing iterables - such as lists or tuples.

They allow for short, concise notation as opposed to loops. As usual, it's important to think about code readability and not overuse them.

See below a quick worksheet with examples of common comprehensions in Python, applied in a simple scenario - computing number squares.

Thanks for reading!

No alt text provided for this image

Found it useful? Subscribe to my Analytics newsletter at notjustsql.com.