Python Comprehensions: List, Dictionary, Set, and Tuple Examples
This article explains Python's comprehension syntax for lists, dictionaries, sets, and tuples, providing clear formats and practical code examples that demonstrate filtering, transformation, and generation of data structures in concise one‑line expressions.
Python comprehensions are a concise way to construct new sequences or collections from existing iterables. The language supports list, dictionary, set, and tuple comprehensions, each following a similar pattern of an expression followed by a for clause and optional if filter.
List comprehension uses square brackets: [expression for item in iterable] or [expression for item in iterable if condition] . Example that filters strings longer than three characters and converts them to uppercase:
names = ['Bob', 'Tom', 'alice', 'Jerry', 'Wendy', 'Smith']
new_names = [name.upper() for name in names if len(name) > 3]
print(new_names)
# Output: ['ALICE', 'JERRY', 'WENDY', 'SMITH']Another example computes all numbers less than 30 divisible by 3:
multiples = [i for i in range(30) if i % 3 == 0]
print(multiples)
# Output: [0, 3, 6, 9, 12, 15, 18, 21, 24, 27]Dictionary comprehension uses curly braces with a key‑value expression: {key_expr: value_expr for item in collection} or with a filter. Example creating a dictionary where each string is a key and its length is the value:
listdemo = ['Google', 'Runoob', 'Taobao']
newdict = {key: len(key) for key in listdemo}
print(newdict)
# Output: {'Google': 6, 'Runoob': 6, 'Taobao': 6}Creating a dictionary of numbers to their squares:
dic = {x: x**2 for x in (2, 4, 6)}
print(dic)
# Output: {2: 4, 4: 16, 6: 36}Set comprehension also uses curly braces but produces a set: {expression for item in iterable} or with a filter. Example generating squares of 1, 2, and 3:
setnew = {i**2 for i in (1, 2, 3)}
print(setnew)
# Output: {1, 4, 9}Example extracting characters from a string that are not in "abc":
a = {x for x in 'abracadabra' if x not in 'abc'}
print(a)
# Output: {'d', 'r'}Tuple comprehension (generator expression) uses parentheses and returns a generator object: (expression for item in iterable) or with a filter. Example generating numbers 1‑9 as a generator and converting it to a tuple:
a = (x for x in range(1, 10))
print(a) # <generator object ...>
tuple_a = tuple(a)
print(tuple_a)
# Output: (1, 2, 3, 4, 5, 6, 7, 8, 9)These comprehension forms enable compact, readable, and efficient data processing in Python, making them essential tools for everyday programming.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.