Python List Comprehension
List comprehension in Python provides a concise way to create lists. It allows generating a new list by applying an expression to each element in an iterable, such as a list, tuple, or range, in a single line of code. This method improves readability and performance compared to traditional loops.
Syntax of List Comprehension
The syntax of list comprehension in Python follows this structure:
[expression for item in iterable if condition]
expression
→ The operation or transformation applied to each item.for item in iterable
→ Loops through the given iterable (e.g., list, tuple, range).if condition
(optional) → Filters elements based on a condition.
Examples
1. Creating a List Using List Comprehension
We can generate a new list using list comprehension instead of using a loop.
# Generating a list of squares using list comprehension
squares = [x**2 for x in range(1, 6)]
# Printing the result
print("List of squares:", squares)
Here, we are creating a list named squares
using list comprehension:
x**2
: This is the expression that calculates the square of each number in the given range.for x in range(1, 6)
: This iterates over the numbers from 1 to 5, applying the square operation.
Output:
List of squares: [1, 4, 9, 16, 25]
2. Using List Comprehension with a Condition
We can also include conditions inside list comprehensions to filter elements.
# Generating a list of even numbers from 1 to 10
even_numbers = [x for x in range(1, 11) if x % 2 == 0]
# Printing the result
print("Even numbers:", even_numbers)
Here, we create a list named even_numbers
that contains only even numbers between 1 and 10:
x
: Represents the current number being processed.for x in range(1, 11)
: Iterates over numbers from 1 to 10.if x % 2 == 0
: Filters the numbers, allowing only those divisible by 2.
Output:
Even numbers: [2, 4, 6, 8, 10]
3. Nested List Comprehension
List comprehension can also be nested to create multi-dimensional lists, such as matrices.
# Creating a 3x3 matrix using list comprehension
matrix = [[j for j in range(1, 4)] for i in range(3)]
# Printing the matrix
print("Matrix:", matrix)
Here, we create a nested list called matrix
using nested list comprehension:
[j for j in range(1, 4)]
: Generates a list containing numbers 1 to 3.for i in range(3)
: Repeats this process 3 times to create 3 rows.
Output:
Matrix: [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
4. List Comprehension with Functions
We can use list comprehension with functions to process data more efficiently.
# Function to double a number
def double(num):
return num * 2
# Using list comprehension with a function
doubled_numbers = [double(x) for x in range(1, 6)]
# Printing the result
print("Doubled numbers:", doubled_numbers)
Here, we define a function double(num)
that returns twice the input value:
double(num)
: Function that takes an input number and returns twice its value.[double(x) for x in range(1, 6)]
: Calls the function for each number from 1 to 5.
Output:
Doubled numbers: [2, 4, 6, 8, 10]
Conclusion
- Python list comprehension provides a shorter syntax than traditional loops.
- Conditions can be applied to filter elements.
- Python list comprehension supports nested structures and function calls.