Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Python Intermediate - Comprehensions

Using comprehensions for concise code in Python

Comprehensions provide a concise way to create lists, dictionaries, and sets in Python. They allow you to generate these collections from existing iterables in a clear and readable manner. This tutorial explores how to use comprehensions to write concise and efficient code in Python.

Key Points:

  • Comprehensions provide a concise way to create lists, dictionaries, and sets.
  • List comprehensions use square brackets and are the most common type.
  • Dictionary comprehensions use curly braces and generate key-value pairs.
  • Set comprehensions use curly braces and generate unique elements.

List Comprehensions

List comprehensions provide a concise way to create lists. They consist of brackets containing an expression followed by a for clause:


# Example of a list comprehension
squares = [x * x 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 * x for x in range(10) if x % 2 == 0]
print(even_squares)  # Output: [0, 4, 16, 36, 64]
            

In this example, a list of squares of numbers from 0 to 9 is created using a list comprehension. The second example shows a list comprehension with a condition to include only even squares.

Dictionary Comprehensions

Dictionary comprehensions provide a concise way to create dictionaries. They consist of curly braces containing a key-value pair followed by a for clause:


# Example of a dictionary comprehension
squares_dict = {x: x * x 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 * x for x in range(10) if x % 2 == 0}
print(even_squares_dict)  # Output: {0: 0, 2: 4, 4: 16, 6: 36, 8: 64}
            

In this example, a dictionary of squares of numbers from 0 to 9 is created using a dictionary comprehension. The second example shows a dictionary comprehension with a condition to include only even squares.

Set Comprehensions

Set comprehensions provide a concise way to create sets. They consist of curly braces containing an expression followed by a for clause:


# Example of a set comprehension
unique_squares = {x * x for x in range(10)}
print(unique_squares)  # Output: {0, 1, 4, 9, 16, 25, 36, 49, 64, 81}

# Set comprehension with condition
even_unique_squares = {x * x for x in range(10) if x % 2 == 0}
print(even_unique_squares)  # Output: {0, 4, 16, 36, 64}
            

In this example, a set of squares of numbers from 0 to 9 is created using a set comprehension. The second example shows a set comprehension with a condition to include only even squares.

Nested Comprehensions

Comprehensions can be nested to create more complex data structures:


# Example of a nested list comprehension
matrix = [[j for j in range(5)] for i in range(3)]
print(matrix)  # Output: [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]

# Example of a nested dictionary comprehension
nested_dict = {i: {j: j * j for j in range(5)} for i in range(3)}
print(nested_dict)  # Output: {0: {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}, 1: {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}, 2: {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}}
            

In this example, a 3x5 matrix is created using a nested list comprehension, and a nested dictionary is created using a nested dictionary comprehension.

Generator Expressions

Generator expressions are similar to comprehensions but produce items lazily. They use parentheses instead of square or curly braces:


# Example of a generator expression
squares_gen = (x * x for x in range(10))

# Using the generator
for square in squares_gen:
    print(square)
            

In this example, a generator expression is used to create a generator that produces the squares of numbers from 0 to 9.

Summary

In this tutorial, you learned about comprehensions in Python, including list comprehensions, dictionary comprehensions, and set comprehensions. Comprehensions provide a concise and readable way to create collections from existing iterables. You also explored nested comprehensions and generator expressions. Using comprehensions effectively can lead to more concise and efficient Python code.