Learn Python list comprehension with clear syntax, beginner-friendly examples, and real-world applications. Discover how to write concise, efficient code for data processing, filtering, and transformations.
List comprehensions are a shorter and cleaner way to create lists. Instead of writing multiple lines with a for loop, you can do the same in just one line.
for
loops in many cases.[expression for item in iterable if condition]
expression
: What you want to do with each itemitem
: The variable representing each element in the iterableiterable
: The sequence you’re looping through (list, tuple, string, etc.)condition
(optional): Filters which items to include# Traditional way
numbers = [1, 2, 3, 4]
squares = []
for num in numbers:
squares.append(num ** 2)
# With list comprehension
squares = [num ** 2 for num in numbers]
# Result: [1, 4, 9, 16]
Python List Comprehension Tutorial – Easy Code Example and Video for Beginners
# Only even numbers
numbers = [1, 2, 3, 4, 5, 6]
evens = [num for num in numbers if num % 2 == 0]
# Result: [2, 4, 6]
🔎 means: “From numbers 1 to 6, pick x only if x is even”
# Squares of even numbers only
numbers = [1, 2, 3, 4, 5, 6]
even_squares = [num ** 2 for num in numbers if num % 2 == 0]
# Result: [4, 16, 36]
# Convert comma-separated string to list of integers
user_input = "1, 2, 3, 4, five, 6"
numbers = [int(x.strip()) for x in user_input.split(',') if x.strip().isdigit()]
# Result: [1, 2, 3, 4, 6]
# Remove empty strings and strip whitespace
data = [" apple ", "banana", " ", "cherry ", ""]
clean_data = [fruit.strip() for fruit in data if fruit.strip()]
# Result: ["apple", "banana", "cherry"]
# Read lines from a file and remove newline characters
with open('data.txt') as file:
lines = [line.strip() for line in file]
# Transpose a matrix (swap rows and columns)
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
transposed = [[row[i] for row in matrix] for i in range(3)]
# Result: [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
# Extract specific fields from API response
api_response = [
{"id": 1, "name": "Alice", "active": True},
{"id": 2, "name": "Bob", "active": False},
{"id": 3, "name": "Charlie", "active": True}
]
active_users = [user["name"] for user in api_response if user["active"]]
# Result: ["Alice", "Charlie"]
While list comprehensions are powerful, they’re not always the best choice:
In these cases, a traditional for
loop might be more appropriate.
Create a list of numbers from 1 to 10. Use list comprehension to generate a new list with the square of each number.
# Expected Output: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
From a list of numbers, create a new list containing only the odd numbers using list comprehension.
# Input: [10, 15, 20, 25, 30, 35]
# Expected Output: [15, 25, 35]
Use list comprehension to get the cube of even numbers from the list.
# Input: [1, 2, 3, 4, 5, 6]
# Expected Output: [8, 64, 216]
Write a list comprehension to multiply only the numbers divisible by 5 by 10.
# Input: [5, 8, 10, 13, 20, 21]
# Expected Output: [50, 100, 200]
Replace all even numbers in a list with the string "Even"
using list comprehension.
# Input: [3, 6, 9, 12, 15]
# Expected Output: [3, 'Even', 9, 'Even', 15]
Given a list of names, create a list of the first letter of each name using list comprehension.
# Input: ["Alice", "Bob", "Charlie"]
# Expected Output: ['A', 'B', 'C']
Convert each string in a list to uppercase using list comprehension.
# Input: ["apple", "banana", "cherry"]
# Expected Output: ["APPLE", "BANANA", "CHERRY"]