List comprehensions allow you to create lists in a concise and readable way. They can often replace the need for longer loops or the [`map`](/tutorials/map) and [`filter`](/tutorials/filter) functions. This tutorial will cover the basics of list comprehensions, advanced uses, and practical examples to help you master this essential feature. ## Basics of List Comprehensions A list comprehension provides a syntactic way to create a list from an existing iterable. The basic syntax is: ```python [main.nopy] [new_item for item in iterable] ``` ### Simple Example Let's start with a simple example of creating a list of squares.
# Using a loop
squares_loop = []
for x in range(10):
squares_loop.append(x**2)
# Using a list comprehension
squares_comprehension = [x**2 for x in range(10)]
print("Squares with loop:", squares_loop)
print("Squares with comprehension:", squares_comprehension)**Explanation:** - The list comprehension `[x**2 for x in range(10)]` creates a new list where each element is the square of the corresponding element from `range(10)`. ### Adding a Condition You can also add an `if` condition to a list comprehension to filter elements.
# List of even squares using a loop
even_squares_loop = []
for x in range(10):
if x % 2 == 0:
even_squares_loop.append(x**2)
# List of even squares using a list comprehension
even_squares_comprehension = [x**2 for x in range(10) if x % 2 == 0]
print("Even squares with loop:", even_squares_loop)
print("Even squares with comprehension:", even_squares_comprehension)**Explanation:** - The condition `if x % 2 == 0` filters out odd numbers, so only the squares of even numbers are included in the list. ### Using Nested Loops You can nest loops within a list comprehension.
# Using nested loops to create a list of tuples
product_loop = []
for x in range(3):
for y in range(3):
product_loop.append((x, y))
# Using a nested list comprehension
product_comprehension = [(x, y) for x in range(3) for y in range(3)]
print("Product with loop:", product_loop)
print("Product with comprehension:", product_comprehension)**Explanation:** - This creates a list of tuples representing all pairs of `x` and `y` values from `0` to `2`. ## Advanced List Comprehensions List comprehensions can go beyond simple examples and are versatile enough to solve many types of problems. ### Flattening a List of Lists You can use a nested list comprehension to flatten a list of lists.
# List of lists
nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
# Flatten the list
flattened = [item for sublist in nested_list for item in sublist]
print("Flattened list:", flattened)**Explanation:** - The list comprehension `[item for sublist in nested_list for item in sublist]` iterates through each sublist and then through each item in those sublists to flatten it into a single list. ### Combining `if` and `else` in List Comprehension You can include `if` and `else` in the expression part of a list comprehension.
# Using `if` and `else` in a list comprehension
number_list = [x if x % 2 == 0 else -x for x in range(10)]
print("Number list with if-else:", number_list)**Explanation:** - This creates a list of numbers where even numbers are kept as is and odd numbers are negated. ## Practical Examples ### Creating a Dictionary from Two Lists You can create a dictionary from two lists using a list comprehension wrapped in a dictionary constructor.
keys = ["name", "age", "city"]
values = ["Alice", 25, "New York"]
# Creating a dictionary from two lists
my_dict = {k: v for k, v in zip(keys, values)}
print("Dictionary:", my_dict)
**Explanation:**
- The `zip(keys, values)` function pairs up elements from `keys` and `values`, and the dictionary comprehension `{k: v for k, v in zip(keys, values)}` constructs the dictionary from these pairs.
### Filtering a List of Strings
You can filter a list of strings based on certain conditions.
# List of strings
fruits = ["apple", "banana", "cherry", "date", "elderberry"]
# Filter fruits to include only those with more than 5 letters
filtered_fruits = [fruit for fruit in fruits if len(fruit) > 5]
print("Filtered fruits:", filtered_fruits)**Explanation:** - The condition `if len(fruit) > 5` filters the fruits to include only those with more than 5 letters. ### Generating a List of Dictionaries You can generate a list of dictionaries using a list comprehension.
names = ["Alice", "Bob", "Charlie"]
ages = [25, 30, 35]
# List of dictionaries
people = [{"name": name, "age": age} for name, age in zip(names, ages)]
print("List of dictionaries:", people)
**Explanation:**
- This list comprehension creates a dictionary for each pair of name and age using `{"name": name, "age": age}` inside the list comprehension.