Python List Comprehension: The Complete Guide
What is Python list comprehension?
Python list comprehension is a concise syntax for creating a list by iterating over an existing sequence and optionally filtering or transforming items. It’s more compact than writing a loop, but it still loops under the hood (the name just doesn’t advertise it). The Python tutorial’s section on list comprehensions covers the same syntax in the official docs.
Instead of this:
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
newL = []
for num in myList:
num = num * 2
newL.append(num)
print(newL)
Output:
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
You can write this:
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
newList = [x * 2 for x in myList]
print(newList)
Output:
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
The syntax is [expression for item in sequence]. It’s more readable and avoids the temporary list and .append() call.
List comprehension with range()
The range() function generates a sequence of numbers. You can use it in a comprehension to create lists of numbers.
Old approach with a loop:
l = []
for i in range(0, 10):
l.append(i)
print(l)
Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
With list comprehension:
l = [i for i in range(0, 10)]
print(l)
Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Much shorter, and the intent is clearer.
Filtering with if
List comprehension lets you filter items by adding an if condition after the for.
Create a list of only even numbers:
l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
newL = [x for x in l if x % 2 == 0]
print(newL)
Output:
[2, 4, 6, 8, 10]
The if x % 2 == 0 part filters out odd numbers. Only items that pass the condition make it into the new list.
You can combine transformation and filtering. For more on Python fundamentals, see Python interview questions:
l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
newL = [x * 2 for x in l if x % 2 == 0]
print(newL)
Output:
[4, 8, 12, 16, 20]
This keeps only even numbers and then doubles them.
List comprehension with strings
Strings are iterable, so you can turn them into lists of characters:
myString = "Hello World"
l = [x for x in myString]
print(l)
Output:
['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']
Each character becomes an element in the list.
List comprehension with tuples
Tuples work the same way:
t = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
l = [x for x in t]
print(l)
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
The syntax is identical. Iterating over a tuple and building a list is straightforward.
List comprehension with functions
You can call a function inside a comprehension to transform each item:
def multiply(x):
return x * 2
l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
newList = [multiply(x) for x in l]
print(newList)
Output:
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
The function must return a value, which becomes the element in the new list.
Dict and set comprehensions
Despite what you might read elsewhere, you can use comprehension syntax with dictionaries and sets.
Dict comprehension:
d = {x: x**2 for x in range(5)}
print(d)
Output:
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
Set comprehension:
s = {x**2 for x in range(5)}
print(s)
Output:
{0, 1, 4, 9, 16}
Dict comprehensions create key: value pairs, and set comprehensions remove duplicates (which is what sets do).
Choosing between a loop and a comprehension
A comprehension is cleaner when you’re building a new list. A regular loop is clearer when your logic gets complex, you have nested conditions, or you need multiple statements per iteration.
Readability wins over brevity. If a comprehension becomes a mental puzzle to parse, write a loop instead. One rule of thumb: if your comprehension doesn’t fit on one line without wrapping awkwardly, break it into a loop.
Are you using comprehensions in your Python code, or do you find yourself reaching for loops most of the time?