Open In App

Reducing Execution time in Python using List Comprehensions

Last Updated : 20 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Python, list comprehensions provide a concise way to create lists while reducing execution time. They replace loops and map/filter functions for many use cases, improving readability and efficiency. The task is to explore different scenarios where list comprehensions can reduce execution time compared to traditional methods.

Using list comprehensions instead of loops

List comprehensions are faster than traditional loops because they are optimized at the implementation level. They allow us to create lists in a single line.

Python
# Initializing a range of numbers
a = range(1, 11)

# Using list comprehension to square each number
res = [x ** 2 for x in a]
print(res)

Output
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

Explanation:

  • We use list comprehension to iterate through the numbers and calculate their squares.
  • This reduces execution time compared to using a for loop with append as the operation happens in a single step.

Let’s explore some more ways and see how to reduce execution time in Python using List Comprehensions.

Using conditional list comprehensions

We can include conditions in list comprehensions to filter elements, avoiding separate if statements in a loop.

Python
# Initializing a range of numbers
a = range(1, 11)

# Using list comprehension to filter even numbers
res = [x for x in a if x % 2 == 0]
print(res)

Output
[2, 4, 6, 8, 10]

Explanation:

  • We add a condition (if x % 2 == 0) directly in the list comprehension to filter even numbers.
  • This avoids the need for an additional check inside a loop, making the code more efficient.
  • Time complexity remains O(n), but execution is faster due to reduced overhead.

Replacing map() and filter() functions

List comprehensions can replace map() and filter() functions, providing a simpler and more Pythonic approach.

Python
# Initializing a range of numbers
a = range(1, 11)

# Using list comprehension to add 5 to each number
res = [x + 5 for x in a]
print(res)

Output
[6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

Explanation:

  • We use list comprehension to add 5 to each element in the list, replacing the need for a map() function.
  • This approach is faster and easier to read as it avoids function calls.

Nested list comprehensions

For multi-dimensional data, nested list comprehensions can replace nested loops, improving both readability and performance.

Python
# Initializing a range of numbers
a = range(1, 4)

# Using nested list comprehension to create pairs
res = [(x, y) for x in a for y in a if x != y]
print(res)

Output
[(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]

Explanation:

  • We create pairs of numbers where the first is not equal to the second.
  • The nested structure allows us to replace two nested loops, reducing execution time.

Combining list comprehensions with functions

List comprehensions can be used with functions to perform complex operations in a single step.

Python
# Defining a function to calculate the cube
def cube(x):
    return x ** 3

# Initializing a range of numbers
a = range(1, 6)

# Using list comprehension to calculate cubes
res = [cube(x) for x in a]
print(res)

Output
[1, 8, 27, 64, 125]

Explanation:

  • We define a function to calculate the cube of a number.
  • Using the function inside a list comprehension reduces the need for additional loops or operations.


Next Article
Practice Tags :

Similar Reads