⚙️ Advanced Python
List, Dict & Set Comprehensions
Comprehensions provide a concise way to create lists, dictionaries, and sets. They are often more efficient and readable than traditional loops.
● Intermediate
📖 Based on: Fluent Python — Luciano Ramalho, Python Cookbook
📋 Table of Contents
1 · List Comprehensions
A list comprehension consists of brackets containing an expression followed by a for clause, then zero or more for or if clauses.
Python
squares = [x**2 for x in range(5)] print(squares) # [0, 1, 4, 9, 16]
🔁 Return Value
A list comprehension returns a new List object containing the results of the expression for each item.