avatarYong Cui

Summary

This article provides a comprehensive guide to mastering list comprehensions in Python, covering basic syntax, conditional filtering, conditional assignment, nested structures, and the use of the walrus operator, as well as set and dictionary comprehensions.

Abstract

The tutorial is designed to help Python learners grasp the intricacies of list comprehensions, which are a powerful and concise method for creating lists. It begins with the fundamental syntax of list comprehensions and demonstrates how they can simplify list creation compared to traditional for loops. The article delves into using conditional statements within list comprehensions for filtering out unwanted items and for conditional assignment, where the condition determines the value assigned to each list item. It also explores the use of nested list comprehensions for more complex list generation tasks, the incorporation of the walrus operator for assignments within expressions, and the extension of the comprehension concept to sets and dictionaries. The guide emphasizes the importance of list comprehensions as a "Pythonic" feature that enhances code readability and efficiency.

Opinions

  • List comprehensions are considered an essential "Pythonic" feature for writing concise and readable code.
  • The use of list comprehensions is preferable to traditional for loops for list creation due to their brevity and clarity.
  • Conditional filtering in list comprehensions is highlighted as a valuable technique for including only items that meet specific criteria.
  • The article suggests that while nested list comprehensions are powerful, they should be used judiciously to maintain code readability.
  • The introduction of the walrus operator in Python 3.8 is seen as an enhancement to list comprehensions, allowing for more complex operations within the expression.
  • The author advocates for the use of set and dictionary comprehensions as they provide similar benefits to list comprehensions when creating sets and dictionaries.

9 Things to Know to Master List Comprehensions in Python

This tutorial will help you learn the most common usages of list comprehensions in Python

Photo by Roman Synkevych on Unsplash

Many more people are starting to learn Python, as it has become one of the most popular programming languages for almost anything, like web development, scientific computing, and certainly artificial intelligence.

No matter where you’re going with Python, you unavoidably have to learn Python’s data structures, variable and function declarations, conditional statements, control flows, and other basic concepts.

One important “Pythonic” feature that can be puzzling to many beginners is the list comprehension a concise way to create lists.

Heard of it before, but don’t know what it is?

Great, this article will provide you with a head-start for mastering list comprehensions in your Python learning adventure.

For the purpose of easier organization, I’ve listed nine things that we should know about list comprehensions, including its syntax and various use cases.

1. Basic Syntax

The most basic list comprehension has the following syntax.

As mentioned previously, it serves as a concise way of doing certain things, such as creating lists. The expanded form is usually expressed as a for loop, in which each item of the iterable runs certain operations as specified in the expression.

# list comprehension
[expression for item in iterable]
# expanded form
for item in iterable:
    expression

2. Create a List

It’s not surprising at all that the most popular usage is to create a list concisely.

Suppose that we don’t know list comprehensions, we’ll probably do something like the below when it comes to the creation of a list. To do that, first, we’ll declare an empty list. Second, in the for loop, we append each item to the list.

As mentioned in the basic syntax section, we can “compress” the for loop statement into one line — using the list comprehension with just one line of code, we can conveniently create a list by iterating the original list.

>>> pets = ('bird', 'snake', 'dog', 'turtle', 'cat', 'hamster')
>>> uppercased_pets = [pet.upper() for pet in pets]
>>> uppercased_pets
['BIRD', 'SNAKE', 'DOG', 'TURTLE', 'CAT', 'HAMSTER']

3. Conditional Statement for Filtering

Sometimes, when we use list comprehensions to create a list, we don’t want to include all of the items on the existing list.

In this case, we need a conditional statement to filter out the items in the existing list that don’t meet certain criteria. The corresponding list comprehension has the following syntax.

Here’s an example of this usage.

>>> primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
>>> squared_primes = [x*x for x in primes if x%10 == 3]
>>> squared_primes
[9, 169, 529]

If we have a more complicated evaluation of the condition, we can even use a function.

4. Conditional Assignment

Sometimes, we don’t want to filter out the items from the original list.

Instead, we want to evaluate the condition to determine which expression is used. The syntax and its usage are given below. The syntax is also explained below.

5. Replace map()

In some situations, you may have seen people use map() to create a list. Specifically, the map() function has the following syntax together with an example of its basic usage.

One thing to note is that the map() function returns an iterable object, and thus we can use the list() function to generate a list from this iterable.

As shown previously, we can replace the map() function with the list comprehension.

>>> pets = ('bird', 'snake', 'dog', 'turtle', 'cat', 'hamster')
>>> uppercased_pets = [pet.upper() for pet in pets]
>>> uppercased_pets
['BIRD', 'SNAKE', 'DOG', 'TURTLE', 'CAT', 'HAMSTER']

6. Nested List Comprehensions

Suppose that we have a tuple in the code snippet below, and we want to create a new list of items that are squares of all numbers in the tuple.

In this case, we can use the nested list comprehension, the syntax of which is also shown below.

Although it’s technically possible to have multiple levels for the nested list comprehensions, for readability, it’s not recommended to have more than two levels.

7. Use Walrus Operator

One of the new features in Python 3.8 is the introduction of the walrus operator (:=), which is used in assignment expression.

Suppose that we want to draw ten times from a list of letters, and the list that we’re creating will only include vowels from these drawings. Here’s how we can do it using the walrus operator in the list comprehension.

Specifically, in the example below, we evaluate whether a random letter drawn from the letters is a vowel, and if it is, it’ll be assigned to the letter to which the list comprehension’s expression can have access.

8. Set Comprehension

Although the list comprehension is known to many people, we can also use comprehension when we create a set. The basic syntax and its usage are shown below.

One major difference is that we use curly braces instead of square brackets. Certainly, by design, the elements in a set won’t have duplicates as opposed to a list where duplicates are allowed.

Please note that we can also use a conditional statement in a set comprehension.

9. Dict Comprehension

We have list and set comprehensions, and you won’t be surprised to learn that Python also has dict comprehension. The basic syntax and its usage are shown in the following code snippet.

Conclusion

This article reviews the basic syntax of list comprehensions and their usage in various scenarios.

Beyond the list comprehension, we also talked about the set and dict comprehensions. These comprehensions allow us to create these basic collection data types very conveniently in Python with better readability.

Programming
Data Science
Software Development
Software Engineering
Python
Recommended from ReadMedium