Python – List Comprehension
A Python list comprehension consists of brackets containing the expression, which is executed for each element along with the for loop to iterate over each element in the Python list.
Python List Comprehension Syntax
newList = [ expression(element) for element in oldList if condition ]
Python List comprehension provides a much more short syntax for creating a new list based on the values of an existing list.
Advantages of List Comprehension
- More time-efficient and space-efficient than loops.
- Require fewer lines of code.
- Transforms iterative statement into a formula.
List Comprehension in Python Example
Iteration with List comprehension
In this example, we are assigning 1, 2, and 3 to the list and we are printing the list.
Python3
# Using list comprehension to iterate through loopList = [character for character in [1, 2, 3]]# Displaying listprint(List) |
Output
[1, 2, 3]
Even list using list comprehension
In this example, we are printing the even numbers from 0 to 10.
Python3
list = [i for i in range(11) if i % 2 == 0]print(list) |
Output
[0, 2, 4, 6, 8, 10]
Matrix using List comprehension
In this example, we are assigning integers 0 to 2 to 3 rows of the matrix and printing it.
Python3
matrix = [[j for j in range(3)] for i in range(3)] print(matrix) |
Output
[[0, 1, 2], [0, 1, 2], [0, 1, 2]]
List Comprehensions vs For Loop
There are various ways to iterate through a list. However, the most common approach is to use the for loop. Let us look at the below example:
Python3
# Empty listList = []# Traditional approach of iteratingfor character in 'Geeks 4 Geeks!': List.append(character)# Display listprint(List) |
Output
['G', 'e', 'e', 'k', 's', ' ', '4', ' ', 'G', 'e', 'e', 'k', 's', '!']
Above is the implementation of the traditional approach to iterate through a list, string, tuple, etc. Now, list comprehension in Python does the same task and also makes the program more simple.
List Comprehensions translate the traditional iteration approach using for loop into a simple formula hence making them easy to use. Below is the approach to iterate through a list, string, tuple, etc. using list comprehension in Python.
Python3
# Using list comprehension to iterate through loopList = [character for character in 'Geeks 4 Geeks!']# Displaying listprint(List) |
Output
['G', 'e', 'e', 'k', 's', ' ', '4', ' ', 'G', 'e', 'e', 'k', 's', '!']
Time Analysis in List Comprehensions and Loop
The list comprehensions in Python are more efficient both computationally and in terms of coding space and time than a for a loop. Typically, they are written in a single line of code. The below program depicts the difference between loops and list comprehension based on performance.
Python3
# Import required moduleimport time# define function to implement for loopdef for_loop(n): result = [] for i in range(n): result.append(i**2) return result# define function to implement list comprehensiondef list_comprehension(n): return [i**2 for i in range(n)]# Driver Code# Calculate time taken by for_loop()begin = time.time()for_loop(10**6)end = time.time()# Display time taken by for_loop()print('Time taken for_loop:', round(end-begin, 2))# Calculate time takens by list_comprehension()begin = time.time()list_comprehension(10**6)end = time.time()# Display time taken by for_loop()print('Time taken for list_comprehension:', round(end-begin, 2)) |
Output
Time taken for_loop: 0.39 Time taken for list_comprehension: 0.35
From the above program, we can see list comprehensions are quite faster than for loop.
Nested List Comprehensions
Nested List Comprehensions are nothing but a list comprehension within another list comprehension which is quite similar to nested for loops. Below is the program which implements nested loop:
Python3
matrix = []for i in range(3): # Append an empty sublist inside the list matrix.append([]) for j in range(5): matrix[i].append(j)print(matrix) |
Output
[[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]
Now by using nested list comprehensions, the same output can be generated in fewer lines of code.
Python3
# Nested list comprehensionmatrix = [[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]]
List Comprehensions and Lambda
Lambda Expressions are nothing but shorthand representations of Python functions. Using list comprehensions with lambda creates an efficient combination. Let us look at the below examples:
In this example, we are inserting numbers from 10 to 50 in the list and printing it.
Python3
# using lambda to print table of 10numbers = []for i in range(1, 6): numbers.append(i*10)print(numbers) |
Output
[10, 20, 30, 40, 50]
Here, we have used for loop to print a table of 10.
Python3
numbers = [i*10 for i in range(1, 6)]print(numbers) |
Output
[10, 20, 30, 40, 50]
Now here, we have used only list comprehension to display a table of 10.
Python3
# using lambda to print table of 10numbers = list(map(lambda i: i*10, [i for i in range(1, 6)]))print(numbers) |
Output
[10, 20, 30, 40, 50]
Finally, we use lambda + list comprehension to display the table of 10. This combination is very useful to get efficient solutions in fewer lines of code for complex problems.
Conditionals in List Comprehension
We can also add conditional statements to the list comprehension. We can create a list using range(), operators, etc. and cal also apply some conditions to the list using the if statement.
Key Points
- Comprehension of the list is an effective means of describing and constructing lists based on current lists.
- Generally, list comprehension is lightweight and simpler than standard list formation functions and loops.
- We should not write long codes for list comprehensions in order to ensure user-friendly code.
- Every comprehension of the list can be rewritten in for loop, but in the context of list interpretation, every for loop can not be rewritten.
Below are some examples which depict the use of list comprehensions rather than the traditional approach to iterate through iterable:
Python List Comprehension using If-else.
In the example, we are checking that from 0 to 7 if the number is even then insert Even Number to the list else insert Odd Number to the list.
Python3
lis = ["Even number" if i % 2 == 0 else "Odd number" for i in range(8)]print(lis) |
Output
['Even number', 'Odd number', 'Even number', 'Odd number', 'Even number', 'Odd number', 'Even number', 'Odd number']
Nested IF with List Comprehension
In this example, we are inserting numbers in the list which is a multiple of 10 to 100, and printing it.
Python3
lis = [num for num in range(100) if num % 5 == 0 if num % 10 == 0]print(lis) |
Output
[0, 10, 20, 30, 40, 50, 60, 70, 80, 90]
Display a square of numbers from 1 to 10
In this example, we are inserting a square from 1 to 10 to list and printing the list.
Python3
# Getting square of number from 1 to 10squares = [n**2 for n in range(1, 11)]# Display square of even numbersprint(squares) |
Output
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
Display Transpose of 2D- Matrix
In this example, we are making a transpose of the matrix using list comprehension.
Python3
# Assign matrixtwoDMatrix = [[10, 20, 30], [40, 50, 60], [70, 80, 90]]# Generate transposetrans = [[i[j] for i in twoDMatrix] for j in range(len(twoDMatrix[0]))]print(trans) |
Output
[[10, 40, 70], [20, 50, 80], [30, 60, 90]]
Toggle the case of each character in a String
In this example, we toggle the case of each character in a given string using the XOR operator with 32 and store the result in a list.
Python3
# Initializing stringstring = 'Geeks4Geeks'# Toggle case of each characterList = list(map(lambda i: chr(ord(i) ^ 32), string))# Display listprint(List) |
Output
['g', 'E', 'E', 'K', 'S', '\x14', 'g', 'E', 'E', 'K', 'S']
Reverse each string in a Tuple
In this example, we are reversing strings in for loop and inserting them into the list, and printing the list.
Python3
# Reverse each string in tupleList = [string[::-1] for string in ('Geeks', 'for', 'Geeks')]# Display listprint(List) |
Output
['skeeG', 'rof', 'skeeG']
Creating a list of Tuples from two separate Lists
In this example, we have created two lists of names and ages. We are using zip() in list comprehension and we are inserting the name and age as a tuple to list. Finally, we are printing the list of tuples.
Python3
names = ["G", "G", "g"]ages = [25, 30, 35]person_tuples = [(name, age) for name, age in zip(names, ages)]print(person_tuples) |
Output:
[('G', 25), ('G', 30), ('g', 35)]Display the sum of digits of all the odd elements in a list.
In this example, We have created a list and we are finding the digit sum of every odd element in the list.
Python3
# Explicit functiondef digitSum(n): dsum = 0 for ele in str(n): dsum += int(ele) return dsum# Initializing listList = [367, 111, 562, 945, 6726, 873]# Using the function on odd elements of the listnewList = [digitSum(i) for i in List if i & 1]# Displaying new listprint(newList) |
Output
[16, 3, 18, 18]



Please Login to comment...