Understanding List Comprehensions in Python 3

Introduction

List comprehensions offer a succinct way to create lists based on existing lists. When using list comprehensions, lists can be built by leveraging any iterable, including strings and tuples.

Syntactically, list comprehensions consist of an iterable containing an expression followed by a for clause. This can be followed by additional for or if clauses, so familiarity with for loops and conditional statements will help you understand list comprehensions better.

List comprehensions provide an alternative syntax to creating lists and other sequential data types. While other methods of iteration, such as for loops, can also be used to create lists, list comprehensions may be preferred because they can limit the number of lines used in your program.

List Comprehensions

In Python, list comprehensions are constructed like so:

list_variable = [x for x in iterable]

A list, or other iterable, is assigned to a variable. Additional variables that stand for items within the iterable are constructed around a for clause. The in keyword is used as it is in for loops, to iterate over the iterable.

Let’s look at an example that creates a list based on a string:

shark_letters = [letter for letter in 'shark']
print(shark_letters)

Here, the new list is assigned to the variable shark_letters, and letter is used to stand in for the items contained in the iterable string 'shark'.

For us to confirm what the new list shark_letters looks like, we call for it to print() and receive the following output:

Output

['s', 'h', 'a', 'r', 'k']

The list we created with the list comprehension is comprised of the items in the string 'shark', that is, one string for each letter.

List comprehensions can be rewritten as for loops, though not every for loop is able to be rewritten as a list comprehension.

Using our list comprehension that created the shark_letters list above, let’s rewrite it as a for loop. This may help us better understand how the list comprehension works.

When creating a list with a for loop, the variable assigned to the list needs to be initialized with an empty list, as it is in the first line of our code block. The for loop then iterates over the item, using the variable letter in the iterable string 'shark'. Within the for loop, each item within the string is added to the list with the list.append(x) method.

Rewriting the list comprehension as a for loop provides us with the same output:

Output

['s', 'h', 'a', 'r', 'k']

List comprehensions can be rewritten as for loops, and some for loops can be rewritten to be list comprehensions to make code more succinct.

Using Conditionals with List Comprehensions

List comprehensions can utilize conditional statements to modify existing lists or other sequential data types when creating new lists.

Let’s look at an example of an if statement used in a list comprehension:

The list comprehension uses the tuple fish_tuple as the basis for the new list called fish_list. The keywords of for and in are used, as they were in the section above, and now an if statement is added. The if statement says to only add those items that are not equivalent to the string 'octopus', so the new list only takes in items from the tuple that do not match 'octopus'.

When we run this, we’ll see that fish_list contains the same string items as fish_tuple except for the fact that the string 'octopus' has been omitted:

Output

['blowfish', 'clownfish', 'catfish']

Our new list therefore has every item of the original tuple except for the string that is excluded by the conditional statement.

The list that is being created, number_list, will be populated with the squared values of each item in the range from 0-9 if the item’s value is divisible by 2. The output is as follows:

Output

[0, 4, 16, 36, 64]

To break down what the list comprehension is doing a little more, let’s think about what would be printed out if we were just calling x for x in range(10). Our small program and output would then look like this:

number_list = [x for x in range(10)]
print(number_list)

Output

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Now, let’s add the conditional statement:

number_list = [x for x in range(10) if x % 2 == 0]
print(number_list)

Output

[0, 2, 4, 6, 8]

The if statement has limited the items in the final list to only include those items that are divisible by 2, omitting all of the odd numbers.

This code is multiplying the items in the first list by the items in the second list over each iteration.

To transform this into a list comprehension, we will condense each of the lines of code into one line, beginning with the x * y operation. This will be followed by the outer for loop, then the inner for loop. We’ll add a print() statement below our list comprehension to confirm that the new list matches the list we created with our nested for loop block above:

Our list comprehension takes the nested for loops and flattens them into one line of code while still creating the exact same list to assign to the my_list variable.

List comprehensions provide us with a succinct way of making lists, enabling us to distill several lines of code into a single line. However, it is worth keeping in mind that the readability of our code should always take precedence, so when a list comprehension line becomes too long or unwieldy, it may be best to break it out into loops.

Conclusion

List comprehensions allow us to transform one list or other sequence into a new list. They provide a concise syntax for completing this task, limiting our lines of code.

List comprehensions follow the mathematical form of set-builder notation or set comprehension, so they may be particularly intuitive to programmers with a mathematical background.

Though list comprehensions can make our code more succinct, it is important to ensure that our final code is as readable as possible, so very long single lines of code should be avoided to ensure that our code is user friendly.

Tutorial Series

Python is an extremely readable and versatile programming language. Written in a relatively straightforward style with immediate feedback on errors, Python offers simplicity and versatility, in terms of extensibility and supported paradigms.

The free How To Code in Python eBook can be used as an Open Educational Resource and an alternative to a textbook in the classroom, as well as be made available for the wider public through libraries.

July 19, 2016

Python is an extremely readable and versatile programming language. While Python 2.7 and Python 3 share many similar capabilities, they should not be thought of as entirely interchangeable. This article will take you through the key differences to consider when choosing on whether to work in Python 2 or Python 3 for your development projects.

July 9, 2018

Python is a flexible and versatile programming language that can be leveraged for many use cases, with strengths in scripting, automation, data analysis, machine learning, and back-end development. This tutorial will guide you through installing Python 3 on your local Ubuntu 18.04 Linux machine and setting up a programming environment via the command line.

This tutorial will get your Ubuntu 18.04 server set up with a Python 3 programming environment. Programming on a server has many advantages and supports collaboration across development projects.

September 14, 2016

This tutorial will walk you through writing a “Hello, World” program in Python 3. The “Hello, World!” program is a classic tradition in computer programming. Serving as a simple and complete first program for beginners, as well as a good program to test systems and programming environments, “Hello, World!” illustrates the basic syntax of programming languages.

June 20, 2017

This tutorial will go over how to work with the Python interactive console and leverage it as a programming tool. Providing access to all of Python’s built-in functions and any installed modules, command history, and auto-completion, the interactive console offers the opportunity to explore Python and the ability to paste code into programming files when you are ready.

March 2, 2017

Comments are lines in computer programs that are ignored by compilers and interpreters. This tutorial will go over how to use comments in your Python program, making your projects more readable for humans and thus more open to collaboration.

This Python tutorial will go over the basics of working with strings, including how to create and print strings, concatenate and replicate strings, and store strings in variables.

September 15, 2016

In this tutorial, we’ll go over some of the ways we can work with Python strings to make sure that all output text is formatted correctly. Topics we will cover include: quotes, apostrophes, multiple lines, escape characters, and raw strings.

September 20, 2016

Python has several built-in functions associated with the string data type. These functions let us easily modify and manipulate strings. In this tutorial, we’ll go over several different functions that we can use to work with strings in Python 3.

September 28, 2016

The Python string data type is a sequence made up of one or more individual characters consisting of letters, numbers, whitespace characters, or symbols. Strings are sequences and can be accessed in the same ways as other sequence-based data types, through indexing and slicing. This tutorial will guide you through how to access strings through indexing and how to slice them through their character sequences; it will also cover counting and character location methods.

This Python 3 tutorial will guide you through converting data types including numbers, strings, tuples and lists, as well as provide examples to help familiarize yourself with different use cases.

October 12, 2016

This tutorial will cover some variable basics and how to best use them within the Python 3 programs you create. We'll go through naming rules and conventions, reassigning variables, multiple assignment, and making local and global variables.

This tutorial will guide you through some of the common uses of string formatters in Python, which can help make your code and program more readable and user friendly.

October 26, 2016

This tutorial will go over operators that can be used with number data types in Python.

November 3, 2016

This tutorial will go through a few of the built-in functions that can be used with numeric data types in Python 3. Becoming familiar with these methods can give you more flexibility when programming. We’ll go over the following functions: abs() for absolute value, divmod() to find a quotient and remainder simultaneously, pow() to raise a number to a certain power, round() to round a number to a certain decimal point, sum() to calculate the sum of the items in an iterable data type.

November 17, 2016

The Boolean data type can be one of two values, either True or False. We use Booleans in programming to make comparisons and to control the flow of the program. In this tutorial, we’ll go over the basics you’ll need to understand how Booleans work, including Boolean comparison and logical operators, and truth tables.

October 28, 2016

This tutorial will go through some of the ways we can work with lists in Python. Lists are great to use when you want to work with many related values. They enable you to keep data together, condense your code, and perform the same methods and operations on multiple values at once. This tutorial will cover some basic processes, including indexing, slicing, modifying, and concatenating lists.

November 10, 2016

In this tutorial, we’ll go through the built-in methods that you can use to work with the list data structure in Python. We’ll go through adding items to and removing items from lists, extending lists, reversing and sorting lists, and more.

January 11, 2017

List comprehensions offer a succinct way to create lists based on existing lists. In this tutorial, we will cover the syntax of list comprehension, which will be an important tool in creating efficient code.

January 18, 2017

A tuple is a data structure that consists of an immutable ordered sequence of elements. Because tuples are immutable, their values cannot be modified. In this tutorial, we will cover some basic processes, including indexing, slicing and concatenating tuples, and the built-in functions that are available when working with these data structures.

January 23, 2017

The dictionary is Python’s built-in mapping type. Dictionaries map keys to values, making key-value pairs that can then store data. In this tutorial, we will go over the dictionary data structure in Python.

January 31, 2017

This tutorial will walk you through installing modules, importing modules, and aliasing modules. Modules are Python .py files that consist of Python code. They can create function definitions and statements that you can reference in other Python .py files or via the Python command line interpreter. In Python, modules are accessed by using the import statement, which tells the current program to bring in the definitions and statements of the other relevant file(s) for its own use.

October 21, 2016

This tutorial will take you through writing conditional statements in the Python programming language.

January 4, 2017

A while loop implements the repeated execution of code based on a given Boolean condition. The code that is in a while block will execute as long as the while statement evaluates to True. In this tutorial, we will go over how while loops work and how to construct them.

January 5, 2017

In this tutorial, we will go over the break, continue, and pass statements in Python, which will allow you to use for and while loops more effectively in your code.

February 27, 2017

A function is a block of instructions that, once defined, both performs an action once the function is called and makes that action available for later use. Functions make code more modular, allowing you to use the same code over and over again. In this tutorial, we’ll go over how to define your own functions to use in your coding projects.

May 5, 2017

In this tutorial, we will cover the syntax of working with *args and **kwargs as parameters within functions to pass a variable number of arguments to a given function. Both can be used improve readability and convenience, and are best for situations where the number of inputs within the argument list will remain relatively small.

In this tutorial, we’ll go through creating classes, instantiating objects, initializing attributes with the constructor method, and working with more than one object of the same class.

March 24, 2017

Object-oriented programming allows for variables to be used at the class or instance level. This tutorial will demonstrate the use of both class and instance variables in object-oriented programming in Python.

March 30, 2017

This tutorial will go through some of the major aspects of inheritance in Python, including how parent classes and child classes work, how to override methods and attributes, how to use the super() function, and how to make use of multiple inheritance.

Polymorphism allows for flexibility and loose coupling so that code can be extended and easily maintained over time.
This tutorial will go through applying polymorphism to classes in Python.

April 25, 2017

In software development, debugging is the process of looking for and resolving issues that prevent computer software from running correctly. The Python debugger pdb provides a debugging environment for Python programs. In this tutorial, we will go over how to work with pdb to implement an interactive debugging environment that you can use with any of your programs written in Python.

April 26, 2017

The Python code module is a useful and quick tool for debugging because it can be used to emulate the interactive interpreter. This tutorial will cover how to work with this module to examine your code.

May 2, 2017

The logging module is part of the standard Python library and provides tracking for events that occur while software runs. You can add logging calls to your code to indicate what events have happened. In this tutorial, we will go over how to work with logging calls to understand the various events that occur from running your program over time.

May 12, 2017

This tutorial will guide you through best practices and considerations to make when migrating code from Python 2 to Python 3, and whether you should maintain code that is compatible with both versions.