Comprehensions in Python provide a concise way to create lists, dictionaries, and sets. They offer a readable and expressive syntax for generating collections using a single line of code. Comprehensions can help make your code more Pythonic and often lead to improved performance and readability.
1. List Comprehensions
List comprehensions allow you to create a new list by applying an expression to each item in an existing iterable (like a list or range). They are often used for generating lists in a more readable and compact manner.
1.1 Syntax
[expression for item in iterable if condition]
where expression
is the value to be included in the new list, item
is the variable representing each element in the iterable
, and condition
is an optional filter that includes only items that satisfy the condition.
1.2 Example
# Creating a list of squares of numbers from 0 to 9
squares = [x**2 for x in range(10)]
print(squares) # Output: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
# List comprehension with condition
even_squares = [x**2 for x in range(10) if x % 2 == 0]
print(even_squares) # Output: [0, 4, 16, 36, 64]
2. Dictionary Comprehensions
Dictionary comprehensions create a new dictionary by applying an expression to each item in an iterable. They are similar to list comprehensions but produce dictionaries.
2.1 Syntax
{key_expression: value_expression for item in iterable if condition}
where key_expression
and value_expression
are expressions to generate dictionary keys and values, respectively.
2.2 Example
# Creating a dictionary where keys are numbers and values are their squares
squares_dict = {x: x**2 for x in range(10)}
print(squares_dict) # Output: {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}
# Dictionary comprehension with condition
even_squares_dict = {x: x**2 for x in range(10) if x % 2 == 0}
print(even_squares_dict) # Output: {0: 0, 2: 4, 4: 16, 6: 36, 8: 64}
3. Set Comprehensions
Set comprehensions create a new set by applying an expression to each item in an iterable. Sets are collections of unique elements, so duplicates are automatically removed.
3.1 Syntax
{expression for item in iterable if condition}
where expression
is the value to be included in the new set, and condition
is an optional filter.
3.2 Example
# Creating a set of squares of numbers from 0 to 9
squares_set = {x**2 for x in range(10)}
print(squares_set) # Output: {0, 1, 4, 9, 16, 25, 36, 49, 64, 81}
# Set comprehension with condition
even_squares_set = {x**2 for x in range(10) if x % 2 == 0}
print(even_squares_set) # Output: {0, 4, 16, 36, 64}
4. Nested Comprehensions
Comprehensions can be nested to handle more complex scenarios, such as generating a list of lists or a dictionary of lists.
4.1 Example of Nested List Comprehensions
# Creating a 3x3 matrix using nested list comprehensions
matrix = [[row * 3 + col for col in range(3)] for row in range(3)]
print(matrix) # Output: [[0, 1, 2], [3, 4, 5], [6, 7, 8]]
4.2 Example of Nested Dictionary Comprehensions
# Creating a dictionary with nested dictionary comprehensions
nested_dict = {row: {col: row * 3 + col for col in range(3)} for row in range(3)}
print(nested_dict) # Output: {0: {0: 0, 1: 1, 2: 2}, 1: {0: 3, 1: 4, 2: 5}, 2: {0: 6, 1: 7, 2: 8}}
5. Summary
Python comprehensions provide a concise way to create lists, dictionaries, and sets by applying expressions to elements of iterables. They are useful for improving code readability and efficiency. List comprehensions, dictionary comprehensions, and set comprehensions can all be used to generate collections in a single line of code, and nested comprehensions can handle more complex scenarios.