Hey! If you love Python and building Python apps as much as I do, let's connect on Twitter or LinkedIn. I talk about this stuff all the time!

Comprehensions in Python

In this tutorial, we’ll delve into the world of comprehensions in Python. You’ll learn how to harness this powerful feature to create lists and dictionaries with concise code. …


Updated July 2, 2023

In this tutorial, we’ll delve into the world of comprehensions in Python. You’ll learn how to harness this powerful feature to create lists and dictionaries with concise code.


Definition of Comprehensions

Comprehensions are a way to create sequences (such as lists or tuples) and mappings (like dictionaries) using an intuitive syntax. This technique allows you to define the elements of a sequence or mapping in a more compact and expressive manner, making your code easier to read and write.

Step-by-Step Explanation: Comprehension Syntax

A comprehension typically consists of three main parts:

  1. Target: The type of object being created (e.g., list, tuple, dictionary).
  2. Expression: The value or values that will be included in the resulting object.
  3. Iteration: The source of data, which can be another list, tuple, string, or even a dictionary.

The basic syntax for comprehensions is as follows:

target = [expression for variable in iteration]

Let’s break this down further using an example:

Example 1: Simple List Comprehension

Suppose we want to create a list of squares of numbers from 1 to 5. We can use the following comprehension:

squares = [x**2 for x in range(1, 6)]
print(squares)  # Output: [1, 4, 9, 16, 25]

In this example:

  • The target is a list ([]).
  • The expression is x**2, which calculates the square of each number.
  • The iteration is range(1, 6), which generates numbers from 1 to 5.

Step-by-Step Explanation: Advanced Comprehensions

We can also create comprehensions with multiple expressions and even conditional statements. Here are a few examples:

Example 2: Multiple Expressions

Let’s create a list of tuples containing the square and cube of each number from 1 to 5:

data = [(x, x**3) for x in range(1, 6)]
print(data)  # Output: [(1, 1), (2, 8), (3, 27), (4, 64), (5, 125)]

In this example:

  • The target is a list of tuples ([]).
  • The expressions are x and x**3, which calculate the square and cube of each number.
  • The iteration is still range(1, 6).

Example 3: Conditional Statements

Suppose we want to create a list of numbers from 1 to 10 that are divisible by 2 or 3. We can use the following comprehension:

numbers = [x for x in range(1, 11) if x % 2 == 0 or x % 3 == 0]
print(numbers)  # Output: [2, 3, 4, 6, 8, 9, 10]

In this example:

  • The target is a list ([]).
  • The expression is x, which simply selects the number.
  • The iteration is range(1, 11).
  • The conditional statement if x % 2 == 0 or x % 3 == 0 filters out numbers that are not divisible by either 2 or 3.

Conclusion

Comprehensions in Python are a powerful feature for creating sequences and mappings. By mastering this technique, you can write more concise and expressive code that is easier to read and maintain. In this tutorial, we’ve explored the basics of comprehensions, including simple list comprehension, multiple expressions, and conditional statements. With practice, you’ll become proficient in using comprehensions to solve a wide range of problems in Python programming.


Further Reading:

  • The official Python documentation for List Comprehensions
  • The official Python documentation for Dictionary Comprehensions

Note: This article is intended for educational purposes only. If you have any questions or need further clarification on any of the concepts, please don’t hesitate to ask!

Stay up to date on the latest in Python, AI, and Data Science

Intuit Mailchimp