List comprehension in python

Last Updated Sep 5, 2024

Python List comprehension is a handy syntax for creating new list based on the items in an existing list.

Written By

python programming, python list

Python's list comprehension syntax makes creating a list from a string or another list quick and simple. A new list can be created quickly by acting on each item in the current List. Comparatively, it takes lesser time to comprehend a list without using a for-loop.

To properly analyze and interact with lists in Python, you frequently need to change, alter, or calculate all of the List's items at once.

Additionally, you might need to start from scratch when making new lists or base them on the data of an existing list. Unlike other iterative techniques, such as for-loops, list comprehension is a quick, concise, and elegant approach to generating lists.

List in Python

Python list is a data type that can hold several items indexed by their positions. Just as the name sounds, it is a container that can store a list of items.

The number of items in a list is referred to as the length of the List. The items are indexed in ascending order starting from index position 0. The first item in the List is at the index position of 0.

Items in a Python list do not have to be of the same data type. It is possible to store numbers, Objects, and strings in the same List.

Square brackets [ ] are used to denote Python list literals. To access an item in a python list, the same bracket notation is used with the index number. list[index].

#list
myList = ["Dog", 1, "Lion", 2, "Elephant", 3]
item1 = myList[0]
length = len(myList)

Two of the commonest operations performed on python lists are iterations and constructing another list from the existing list based on some conditions. In such operations, a for-in loop or use of python's inbuilt filter and map functions would come to mind.

myList = ["dog", 1, "cat", 2, "elephant", 3];
anotherList = [];

// create another list by iteration
for item in myList:
  if type(item) is int:
    anotherList.append(item);

// create another list by using python's filter
anotherList2 = filter(lambda item: type(item) is int, myList)

In the example above, the finest and most concise and readable option is the use of filter or map functions. But it also comes with another overhead because of the lambda function.

Python list comprehension makes for a better, simpler, and more readable approach for creating a new list with support for nested lists.

Python List Comprehension Syntax

A concise and much verbose representation of Python List comprehension syntax can be shown below with support for multiple iterations:

python_list = [ expression for expr1 in sequence1
             for expr2 in sequence2 ...
             for exprN in sequenceN
             if condition]

# note that the for in loops are not executed in parrallel
# below is the execution sequence of the loops

for expr1 in sequence1:
    for expr2 in sequence2:
    ...
        for exprN in sequenceN:
             if (condition):
                  # Append the value of the expression to the new list

Python list comprehension syntax can be broken down into 3 sections or segments:

  1. The expression section. The result from evaluating this section is added to the new list.
  2. The for-in section - the for-in section defines the iterations, and creates variables which is accessible in the three sections. It starts from the first for keyword within the bracket notation.
  3. The conditional section - this section is optional. It defines conditions on which the expression should be evaluated. It starts from the first if statement within the bracket notation

In most cases, list comprehension is used to iterate over a single list and the simplified syntax below would be used.

python_list = [expression for variable in iterable if condition]

Below is a breakdown of the syntax and characteristics of python List comprehension:

  1. Python list comprehensions start and end with square brackets [ ].
  2. Python list comprehension syntax creates another list from the result of the operation.
  3. The variables created in the for-in sections are accessible in the expression and conditional sections,
  4. The expression is the operation that needs to be evaluated on each iteration as long as the condition is met. The result of the evaluation expression is added to the new list.
  5. The conditional is optional.
  6. Python list comprehension syntax makes use of the for-in loop.
  7. Python list comprehension works on iterables. Thus, strings, tuples, and sets can be operated using python list comprehension syntax.
  8. Python list comprehension supports neater single-line nested operations on multiple iterables

Below is an example of extracting all items of type int from a python list.

myList = ["dog", 1, "cat", 2, "elephant", 3]
intList = [item for item in myListem if type(item) is int]

print(anotherList)
#output: [1, 2, 3]

As shown above, List comprehension syntax is more elegant, and easily written in a one-line code than normal for-in loop.

Advantages of Python List comprehension

The advantages of list comprehension in python over other iteration methods, such as for loop, filter, and map functions, are listed below:

  1. It is more cost and space effective.
  2. List comprehension code syntax is less verbose; it is just a one-line code
  3. It does not have restrictions of Python scopes, unlike in filter and map methods that would need a lambda function
  4. Nested loops make Python List comprehension the best option when creating a list from two or more iterables
even_numbers = [i for i in range(21) if i % 2 == 0]
print(even_numbers)

#output: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

Conditionals in Python's list comprehension

List comprehensions can alter an existing list using conditional statements. The expression will be evaluated, and the result will be added to the new list only when the condition is satisfied.

Conditionals can also be used in the expression section, making list comprehension powerful and dynamic.

numbers_list = [x for x in range(100) if y % 2 == 0 if y % 5 == 0]
print(numbers_list)

#output: [0, 10, 20, 30, 40, 50, 60, 70, 80, 90]

Python range function

Python's inbuilt range(N) function creates a list from 0 to N-1. There are other overloads of range, including range(start, end), which creates a list from start to end - 1. The last overload is range(start, end, step).

my_list = ["even" if i%2==0 else "odd" for i in range(10)]
print(my_list)

#output: ['even', 'odd', 'even', 'odd', 'even', 'odd', 'even', 'odd', 'even', 'odd']

Nested Python list comprehension - Syntax and examples

As stated earlier, List comprehension in Python supports creating a new list from multiple iterables. It is possible to run nested for loops on multiple iterables using list comprehension.

The expression section can also be a list comprehension, and this makes python list comprehension powerful.

For instance, a nested loop that spans many lines of code is needed to compute the transpose of a matrix. Nested list comprehension suits this use case and can be achieved in one line of code.

Transpose of a Matrix

The transpose of a matrix is another matrix that is formed by changing the matrix's rows into columns and columns into rows. The result of transposing an M x N matrix is an N x M matrix

mat = [[1, 2, 3, 4], [4, 5, 6, 8]]
transposed_matrix = []

#transpose of matrix calculated using for loop
for i in range(len(mat[0])):
    transposed_row = []

    for row in mat:
        transposed_row.append(row[i])
    transposed_matrix.append(transposed_row)

# transpose of matrix computed using nested list comprehension
transposed_matrix = [[row[colIndex] for row in mat] for colIndex in range(len(mat))]

Conclusion

Python List comprehension is a more concise, elegant, and readable syntax for creating a new list from an existing list. This does not mean that using built-in iterable python functions like map and filter is bad.

List comprehension is supported in Python 2.0 and should be fine for your projects because of its wide support.