Python - List Comprehension - GeeksforGeeks (2024)

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.

Example:

Python

numbers = [12, 13, 14,]

doubled = [x *2 for x in numbers]

print(doubled)

Output

[24, 26, 28]

Python List Comprehension Syntax

Syntax: newList = [ expression(element) for element in oldList if condition ]

Parameter:

  • expression: Represents the operation you want to execute on every item within the iterable.
  • element: The term “variable” refers to each value taken from the iterable.
  • iterable: specify the sequence of elements you want to iterate through.(e.g., a list, tuple, or string).
  • condition: (Optional) A filter helps decide whether or not an element should be added to the new list.

Return:The return value of a list comprehension is a new list containing the modified elements that satisfy the given criteria.

Python List comprehension provides a much more short syntax for creating a new list based on the values of an existing list.

List Comprehension in Python Example

Here is an example of using list comprehension to find the square of the number in Python.

Python

numbers = [1, 2, 3, 4, 5]

squared = [x ** 2 for x in numbers]

print(squared)

Output

[1, 4, 9, 16, 25]

Iteration with List Comprehension

In this example, we are assigning 1, 2, and 3 to the list and we are printing the list using List Comprehension.

Python

# Using list comprehension to iterate through loop

List = [character for character in [1, 2, 3]]

# Displaying list

print(List)

Output

[1, 2, 3]

Even list using List Comprehension

In this example, we are printing the even numbers from 0 to 10 using List Comprehension.

Python

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 using List Comprehension.

Python

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:

Python

# Empty list

List = []

# Traditional approach of iterating

for character in 'Geeks 4 Geeks!':

List.append(character)

# Display list

print(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.

Python

# Using list comprehension to iterate through loop

List = [character for character in 'Geeks 4 Geeks!']

# Displaying list

print(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.

Python

# Import required module

import time

# define function to implement for loop

def for_loop(n):

result = []

for i in range(n):

result.append(i**2)

return result

# define function to implement list comprehension

def 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.39Time 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:

Python

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.

Python

# Nested list comprehension

matrix = [[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.

Python

# using lambda to print table of 10

numbers = []

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.

Python

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.

Python

# using lambda to print table of 10

numbers = 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.

Python

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.

Python

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.

Python

# Getting square of number from 1 to 10

squares = [n**2 for n in range(1, 11)]

# Display square of even numbers

print(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.

Python

# Assign matrix

twoDMatrix = [[10, 20, 30],

[40, 50, 60],

[70, 80, 90]]

# Generate transpose

trans = [[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.

Python

# Initializing string

string = 'Geeks4Geeks'

# Toggle case of each character

List = list(map(lambda i: chr(ord(i) ^ 32), string))

# Display list

print(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.

Python

# Reverse each string in tuple

List = [string[::-1] for string in ('Geeks', 'for', 'Geeks')]

# Display list

print(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.

Python

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.

Python

# Explicit function

def digitSum(n):

dsum = 0

for ele in str(n):

dsum += int(ele)

return dsum

# Initializing list

List = [367, 111, 562, 945, 6726, 873]

# Using the function on odd elements of the list

newList = [digitSum(i) for i in List if i & 1]

# Displaying new list

print(newList)

Output

[16, 3, 18, 18]

Advantages of List Comprehension

  • More time-efficient and space-efficient than loops.
  • Require fewer lines of code.
  • Transforms iterative statement into a formula.

Python List Comprehension Exercise Questions

Below are two exercise questions on Python list comprehension. We have covered basic list comprehension code to find the cube of numbers and code to find the length of a word using list comprehension and the len() function.

Q1. Cube of numbers exercise question using list comprehension

Python

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

cube = [number**3 for number in numbers]

print(cube)

Output

[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]

Q2. Finding word length exercise question using list comprehension

Python

words = ["apple", "banana", "cherry", "orange"]

word_lengths = [len(word) for word in words]

print(word_lengths)

Output

[5, 6, 6, 6]


riturajsaha

Improve

Previous Article

Python List Slicing

Next Article

Python List Comprehension and Slicing

Please Login to comment...

Python - List Comprehension - GeeksforGeeks (2024)

FAQs

Is Python list comprehension faster than for? ›

List comprehensions are faster than for loops to create lists. But, this is because we are creating a list by appending new elements to it at each iteration.

Is Python list comprehension lazy? ›

There's no lazy evaluation of lists in Python. List comprehensions simply create a new list. If you want "lazy" evaluation, use a generator expression instead.

Should you use list comprehension in Python? ›

List comprehensions are useful and can help you write elegant code that's easy to read and debug, but they're not the right choice for all circ*mstances. They might make your code run more slowly or use more memory.

Which is faster lambda or list comprehension? ›

List comprehensions are fastest, followed by filter() with a predefined filter function, and filter() with an ad-hoc lambda function comes in last. Again, this is due to the overhead incurred by lambda functions requiring the creation of a new function object at runtime.

What is faster map or list comprehension Python? ›

map() used with a lambda function is usually slower than the equivalent list comprehension. But if you use it with a named function instead, it gets faster.

Are list comprehensions readable? ›

Python's list comprehensions offer a concise and powerful way to create lists. They allow you to express complex operations in a single line of code, making your code more readable and efficient.

What is the time complexity of list comprehension Python? ›

It's at least O(n) where n is the number of elements in the list you start out with. It could be worse complexity like if those elements were strings of size m and you're searching them for a letter, the complexity would be O(nm). It will change list comprehension to list comprehension.

Which is faster list comprehension or generator? ›

Performance benefits of List Comprehensions. List comprehensions are usually faster than generator expressions as generator expressions create another layer of overhead to store references for the iterator. However, the performance difference is often quite small.

What is the advantage of list comprehension in Python? ›

Advantages of Python List Comprehensions:
  • Conciseness and Readability. Python list comprehensions allow you to express complex operations on lists in a single line of code. ...
  • Efficiency. ...
  • Expressive Filtering and Mapping. ...
  • Versatility. ...
  • Limited Expressiveness. ...
  • Debugging Challenges.
Sep 10, 2023

What are the 4 types of comprehension in Python? ›

There are four types of comprehension in Python for different data types – list comprehension, dictionary comprehension, set comprehension, and generator comprehension.

Can you break a list comprehension Python? ›

Overview of using 'break' in List Comprehension. The break statement is not natively supported by list comprehensions, despite the fact that they are a powerful Python feature for creating concise lists.

Is list comprehension fast Python? ›

List comprehensions are often faster than loops because they use a more optimized internal mechanism for iterating over the collection. Additionally, list comprehensions allow you to perform transformations and filtering in a single statement, which can lead to more efficient code.

What is more efficient than a list in Python? ›

Tuples are faster than lists in terms of performance. This is because tuples are immutable, and Python does not need to allocate additional memory to accommodate any changes made to them. Lists, on the other hand, are mutable, and Python needs to allocate additional memory every time you modify them.

What is a list comprehension equivalent to? ›

List comprehension is a more laconic alternative to for-loops (including the nested ones), lambda function, Python built-in functions map(), filter() and reduce(), and conditionals.

Are lists faster in Python? ›

Use Lists Not Arrays

Python's dictionaries and lists make for faster code; use them instead. Python arrays are a wrapper around a C array, and there's extra overhead in converting each array element to a Python object.

What is faster for lookups in Python dictionaries or lists? ›

The fastest way to repeatedly lookup data with millions of entries in Python is using dictionaries. Because dictionaries are the built-in mapping type in Python thereby they are highly optimized.

Is dictionary comprehension faster than for loop in Python? ›

I hope I've convinced you by now that dictionary comprehension is one of the best ways to build dictionaries from an iterable. This method is faster than passing a list of tuples to a dict() function. And while it's not really that much faster than a simple for loop, dictionary comprehension is much more readable.

Top Articles
Latest Posts
Article information

Author: Jonah Leffler

Last Updated:

Views: 6711

Rating: 4.4 / 5 (45 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Jonah Leffler

Birthday: 1997-10-27

Address: 8987 Kieth Ports, Luettgenland, CT 54657-9808

Phone: +2611128251586

Job: Mining Supervisor

Hobby: Worldbuilding, Electronics, Amateur radio, Skiing, Cycling, Jogging, Taxidermy

Introduction: My name is Jonah Leffler, I am a determined, faithful, outstanding, inexpensive, cheerful, determined, smiling person who loves writing and wants to share my knowledge and understanding with you.