Home ยป Python List Comprehension : A Tutorial

Python List Comprehension : A Tutorial

Java SE 11 Programmer II [1Z0-816] Practice Tests
Java SE 11 Programmer I [1Z0-815] Practice Tests
Oracle Java Certification
1 Year Subscription
Java SE 11 Developer (Upgrade) [1Z0-817]
Spring Framework Basics Video Course

List comprehension is a concise and powerful way to create lists in Python. It allows you to generate new lists by applying an expression to each element in an existing list (or other iterable) in just one line.

List comprehension makes your code more readable, efficient, and Pythonic.

This tutorial will cover the basics of list comprehension, how to apply conditional logic, work with nested loops, and more, with practical examples.

1. Basic Syntax of List Comprehension

The basic syntax of list comprehension is:

new_list = [expression for item in iterable]
expression: The operation to be applied on each item.
item: Each element in the iterable (e.g., a list or range).
iterable: The collection to iterate over.

Example: Creating a List of Squares
# Example: Create a list of squares using list comprehension
numbers = [1, 2, 3, 4, 5]

# Generate a new list with squares of each number
squares = [x**2 for x in numbers]

print(squares)  # Output: [1, 4, 9, 16, 25]

In this example:

x**2 is the expression, and x is each element in numbers. The list comprehension creates a new list of squares.

2. List Comprehension with Conditional Logic

You can add an if condition to filter elements in list comprehension. Only the elements that meet the condition are included in the new list.

Example: Filtering Even Numbers

# Example: Filter only even numbers using list comprehension
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]

# Create a new list of even numbers
even_numbers = [x for x in numbers if x % 2 == 0]

print(even_numbers)  # Output: [2, 4, 6, 8]

In this example:

The condition if x % 2 == 0 filters out the even numbers, and only those numbers are added to even_numbers.

3. List Comprehension with if-else

You can use if-else within list comprehension to apply different expressions based on a condition.

Example: List of Even/Odd Labels

# Example: Create a list of labels for even and odd numbers
numbers = [1, 2, 3, 4, 5]

# Create a new list that labels numbers as 'even' or 'odd'
labels = ['even' if x % 2 == 0 else 'odd' for x in numbers]

print(labels)  # Output: ['odd', 'even', 'odd', 'even', 'odd']

In this example:

‘even' if x % 2 == 0 else ‘odd' is an expression that applies different logic based on whether x is even or odd.

4. List Comprehension with Nested Loops

List comprehension can include multiple for loops, allowing you to work with nested structures, such as a matrix or Cartesian product.

Example: Cartesian Product of Two Lists

# Example: Cartesian product of two lists using list comprehension
list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']

# Generate a list of tuples representing the Cartesian product
cartesian_product = [(x, y) for x in list1 for y in list2]

print(cartesian_product)  # Output: [(1, 'a'), (1, 'b'), (1, 'c'), (2, 'a'), (2, 'b'), (2, 'c'), (3, 'a'), (3, 'b'), (3, 'c')]

In this example:

The list comprehension uses two for loops to combine each element of list1 with each element of list2.

Example: Flattening a Matrix

# Example: Flatten a 2D matrix into a 1D list
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

# Flatten the matrix
flattened = [item for row in matrix for item in row]

print(flattened)  # Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]

In this example:

The list comprehension iterates over each row of the matrix and then over each element in the row to create a flattened 1D list.

5. List Comprehension with Functions

You can apply functions to each element inside a list comprehension.

Example: Applying a Function to Each Element

# Example: Using a function to double each number in a list
def double(x):
    return x * 2

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

# Apply the function to each element
doubled = [double(x) for x in numbers]

print(doubled)  # Output: [2, 4, 6, 8, 10]

In this example:

The double() function is applied to each element of the numbers list using list comprehension.

6. List Comprehension with Nested List Comprehension

You can even nest list comprehensions within each other for more complex operations.

Example: Transposing a Matrix

# Example: Transpose a matrix using nested list comprehension
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

# Transpose the matrix
transposed = [[row[i] for row in matrix] for i in range(len(matrix[0]))]

print(transposed)  # Output: [[1, 4, 7], [2, 5, 8], [3, 6, 9]]

In this example:

The outer list comprehension iterates over the indices of the matrix columns, while the inner list comprehension retrieves the elements from each row to perform the transpose.

7. List Comprehension with Complex Conditions

You can include more complex conditions inside a list comprehension to filter elements based on multiple criteria.

Example: Filtering Numbers with Multiple Conditions

# Example: Filtering numbers divisible by both 2 and 3
numbers = list(range(1, 21))

# Create a list of numbers divisible by both 2 and 3
divisible_by_2_and_3 = [x for x in numbers if x % 2 == 0 and x % 3 == 0]

print(divisible_by_2_and_3)  # Output: [6, 12, 18]

In this example:

x % 2 == 0 and x % 3 == 0 filters numbers that are divisible by both 2 and 3.

8. Using List Comprehension for String Manipulation

List comprehension can be used for tasks like splitting, joining, or modifying strings.

Example: Converting Strings to Uppercase

# Example: Converting a list of strings to uppercase
words = ['apple', 'banana', 'cherry']

# Convert each string to uppercase
uppercase_words = [word.upper() for word in words]

print(uppercase_words)  # Output: ['APPLE', 'BANANA', 'CHERRY']

In this example:

The list comprehension converts each string to uppercase using the upper() method.

9. List Comprehension with else Statements

You can incorporate an else clause in list comprehension with an if-else expression inside the list comprehension.

Example: Replacing Even and Odd Numbers

# Example: Replacing even numbers with 'even' and odd numbers with 'odd'
numbers = [1, 2, 3, 4, 5]

# Use list comprehension with an if-else expression
labels = ['even' if x % 2 == 0 else 'odd' for x in numbers]

print(labels)  # Output: ['odd', 'even', 'odd', 'even', 'odd']

In this example:

The list comprehension uses the if-else expression to label each number as either ‘even' or ‘odd'.

10. Performance of List Comprehension

List comprehension is typically faster than traditional for loops because it is optimized internally in Python. However, if the logic becomes too complex, it may affect readability, so you should balance readability with performance.

Example: Timing List Comprehension vs. for Loop

import time

# Example: Timing list comprehension vs. for loop
numbers = list(range(1000000))

# List comprehension
start_time = time.time()
squares_lc = [x**2 for x in numbers]
print("List comprehension time:", time.time() - start_time)

# Traditional for loop
start_time = time.time()
squares_loop = []
for x in numbers:
    squares_loop.append(x**2)
print("For loop time:", time.time() - start_time)

In this example:

You can compare the performance of list comprehension vs. a traditional for loop using the time module.

Summary

List comprehension is a concise way to create lists in Python by applying an expression to elements in an iterable.
You can add if conditions to filter elements, and use if-else expressions to apply different logic based on conditions.
List comprehension supports nested loops and can handle more complex structures like matrices and lists of dictionaries.
It allows you to apply functions and string manipulations directly on list elements.
Nested list comprehension can be used for tasks like matrix transposition and flattening nested lists.
List comprehension is generally more efficient than traditional loops, though complex logic might reduce readability.

By mastering list comprehension, you can write more concise, readable, and Pythonic code for handling lists and iterables.

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More