Comprehensions are a concise way to create lists, sets, and dictionaries using a single line of code. They provide a readable and efficient way of performing common operations on collections like lists, tuples, sets, and dictionaries.
1. List Comprehensions in Python
List comprehension is a compact way of creating lists. It consists of brackets containing an expression followed by a for clause, and can optionally include if conditions.
Syntax:
[expression for item in iterable if condition]
expression: the value to be included in the new list.
item: the current item in the iteration.
iterable: any iterable (list, string, tuple, etc.).
condition: optional condition that filters elements.
Example 1: Create a List of Squares
numbers = [1, 2, 3, 4, 5]
squares = [x ** 2 for x in numbers]
print(squares)
Output:
[1, 4, 9, 16, 25]
Example 2: Filter Even Numbers
numbers = [1, 2, 3, 4, 5, 6]
even_numbers = [x for x in numbers if x % 2 == 0]
print(even_numbers)
Output:
[2, 4, 6]
2. Tuple Comprehensions in Python
Python does not have a specific tuple comprehension, but you can use a generator expression which is similar to list comprehension. Since a tuple is an immutable sequence, the comprehension syntax for tuples is the same as list comprehension but enclosed in parentheses ().
Example 1: Tuple of Squares
numbers = [1, 2, 3, 4, 5]
squares = tuple(x ** 2 for x in numbers)
print(squares)
Output:
(1, 4, 9, 16, 25)
Example 2: Tuple of Even Numbers
numbers = [1, 2, 3, 4, 5, 6]
even_numbers = tuple(x for x in numbers if x % 2 == 0)
print(even_numbers)
Output:
(2, 4, 6)
3. Set Comprehensions in Python
Set comprehensions work similarly to list comprehensions, but they use curly braces {} instead of square brackets []. A set does not allow duplicates, so it automatically filters out any repeated elements.
Syntax:
{expression for item in iterable if condition}
Example 1: Set of Squares
numbers = [1, 2, 3, 4, 5]
squares = {x ** 2 for x in numbers}
print(squares)
Output:
{1, 4, 9, 16, 25}
Example 2: Set of Even Numbers
numbers = [1, 2, 3, 4, 5, 6]
even_numbers = {x for x in numbers if x % 2 == 0}
print(even_numbers)
Output:
{2, 4, 6}
4. Dictionary Comprehensions in Python
Dictionary comprehensions allow you to create dictionaries using a single line of code. The syntax is similar to list comprehensions but uses curly braces {} and key-value pairs.
Syntax:
{key: value for item in iterable if condition}
key: the key in the dictionary.
value: the value associated with the key.
condition: optional condition to filter items.
Example 1: Create a Dictionary of Squares
numbers = [1, 2, 3, 4, 5]
squares_dict = {x: x ** 2 for x in numbers}
print(squares_dict)
Output:
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
Example 2: Filter Odd Numbers into a Dictionary
numbers = [1, 2, 3, 4, 5]
odd_dict = {x: x ** 2 for x in numbers if x % 2 != 0}
print(odd_dict)
Output:
{1: 1, 3: 9, 5: 25}
Summary of Comprehensions
List Comprehension: [expression for item in iterable if condition]
Tuple Comprehension: (expression for item in iterable if condition) (generator expression wrapped in parentheses)
Set Comprehension: {expression for item in iterable if condition}
Dictionary Comprehension: {key: value for item in iterable if condition}
0 Comments