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!

https://media.licdn.com/dms/image/D4D22AQEULgDscs8OxQ/feedshare-shrink_1280/0/1701875506201?e=1704931200&v=beta&t=lew0sFLl38Qn50h6-nHSMC9pfX6menfmPFSuSDFwnlw