Skip to main content

Command Palette

Search for a command to run...

Comprehensions in Python

Updated
1 min read
Comprehensions in Python
C

Senior Data Engineer • Contractor / Freelancer • GCP & AWS Certified


title: "Comprehensions in Python" seoTitle: "Python Comprehensions: List, Dict, Set, and Generator" seoDescription: "A concise guide to list, dictionary, set, and generator comprehensions in Python with practical examples. Learn when comprehensions improve code..." datePublished: Wed Dec 06 2023 22:18:07 GMT+0000 (Coordinated Universal Time) cuid: clpubyrlh000008jn7u8cdbhd slug: comprehensions-in-python cover: https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/nVqNmnAWz3A/upload/810ea2a007ec5ba0b522c1d4e521bf48.jpeg tags: programming-blogs, python, data-engineering, programming-tips


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!

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


Enjoyed this? Here are some related articles you might find useful:

More from this blog

D

Datawise: A blog on SQL, BigQuery, Analytics, Python Tips and Tricks

204 posts

Data Engineer with a passion for transforming complex data landscapes into insightful stories. Here on my blog, I share insights, challenges, and the ever-evolving dance of technology and business.