Python – List Comprehension
Python List Comprehension
List comprehension is a concise and efficient way to create new lists by applying an expression to each item in an existing iterable (like a list, tuple, etc.). It can also include conditions for filtering items.
The basic syntax of a list comprehension is:
[expression for item in iterable]
You can also add conditions to the comprehension:
[expression for item in iterable if condition]
Basic Examples:
- Creating a new list by applying an operation to each element:
my_list = [1, 2, 3, 4, 5]
# Create a new list with each item doubled
new_list = [x * 2 for x in my_list]
print(new_list) # Output: [2, 4, 6, 8, 10]
- List comprehension with a condition (filtering):
my_list = [1, 2, 3, 4, 5]
# Create a new list with only even numbers
new_list = [x for x in my_list if x % 2 == 0]
print(new_list) # Output: [2, 4]
- List comprehension with an
elsestatement:
You can also use an else statement to include an alternative expression when the condition is not met.
my_list = [1, 2, 3, 4, 5]
# Create a new list where even numbers are squared, and odd numbers are multiplied by 2
new_list = [x**2 if x % 2 == 0 else x * 2 for x in my_list]
print(new_list) # Output: [2, 4, 6, 16, 10]
More Complex Examples:
- List comprehension with multiple conditions:
You can add multiple conditions in the if statement to filter the items more precisely.
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Create a new list with numbers divisible by both 2 and 3
new_list = [x for x in my_list if x % 2 == 0 and x % 3 == 0]
print(new_list) # Output: [6]
- List comprehension for nested loops:
You can also use list comprehension for nested loops to handle more complex cases like iterating over a list of lists.
nested_list = [[1, 2], [3, 4], [5, 6]]
# Create a new list containing all items from the sublists
new_list = [item for sublist in nested_list for item in sublist]
print(new_list) # Output: [1, 2, 3, 4, 5, 6]
- List comprehension with a function:
You can apply a function to each item while creating the new list.
my_list = ["apple", "banana", "cherry"]
# Create a new list with the uppercase version of each string
new_list = [fruit.upper() for fruit in my_list]
print(new_list) # Output: ['APPLE', 'BANANA', 'CHERRY']
- Nested list comprehension:
You can also use list comprehension to create a list of lists.
# Create a 2D matrix using list comprehension
matrix = [[row * col for col in range(1, 4)] for row in range(1, 4)]
print(matrix)
# Output:
# [
# [1, 2, 3],
# [2, 4, 6],
# [3, 6, 9]
# ]
Benefits of List Comprehension:
- Compact: It allows you to perform operations in a single line of code, reducing the need for loops and making the code cleaner and more readable.
- Faster: List comprehensions are typically faster than using regular loops because they are optimized for creating lists.
Summary:
- Basic Syntax:
[expression for item in iterable] - With Condition:
[expression for item in iterable if condition] - With
else:[expression if condition else alternate for item in iterable] - Multiple Conditions: You can combine multiple conditions in the
ifstatement. - Nested Loops: You can loop through multiple lists with nested comprehensions.
- Functions: You can apply a function to each item in the list comprehension.
List comprehensions are a powerful tool for generating new lists in a concise and efficient manner.