Conditional and Nested Comprehensions
This section covers advanced comprehension techniques, including conditional expressions (ternary operators) within comprehensions and complex nested patterns.
Conditional Expressions in Comprehensions
You can use conditional expressions (ternary operators) in comprehensions to apply different expressions based on conditions:
numbers = [n if n % 2 == 0 else n * 2 for n in range(5)]print(numbers) # [0, 2, 2, 6, 4]This keeps even numbers as-is and doubles odd numbers.
Combining Conditions and Conditional Expressions
You can combine filtering conditions with conditional expressions:
result = [n * 2 if n > 5 else n for n in range(10) if n % 2 == 0]print(result) # [0, 2, 4, 12, 16]This:
- Filters to only even numbers (
if n % 2 == 0) - Doubles numbers greater than 5, otherwise keeps them as-is
Nested Comprehensions with Conditions
You can have multiple levels of nesting with conditions at each level:
matrix = [[i * j for j in range(5) if j > 0] for i in range(3) if i > 0]print(matrix) # [[1, 2, 3, 4], [2, 4, 6, 8]]This creates a matrix where:
- Outer loop:
ifrom 0 to 2, but only wheni > 0 - Inner loop:
jfrom 0 to 4, but only whenj > 0 - Value:
i * j
Complex Nested Patterns
You can create complex data structures with multiple levels of nesting:
# Create a list of dictionariesdata = [{f"key_{i}": i * j for j in range(3)} for i in range(3)]print(data)# [{'key_0': 0, 'key_0': 0, 'key_0': 0}, {'key_1': 0, 'key_1': 1, 'key_1': 2}, ...]Multiple Conditions in Nested Comprehensions
You can apply conditions at multiple levels:
result = [ (i, j, k) for i in range(5) if i % 2 == 0 for j in range(5) if j % 2 == 1 for k in range(5) if k > 2]print(result[:5]) # [(0, 1, 3), (0, 1, 4), (0, 3, 3), ...]Exercises
Exercise 1: Conditional Expression
Use list comprehension with a conditional expression to create a list where even numbers are kept as-is and odd numbers are squared.
Conditional Expression
numbers = [n if n % 2 == 0 else n * n for n in range(10)]print(numbers)Exercise 2: Multiple Conditions
Use list comprehension to create a list of numbers from 1 to 20, but replace numbers divisible by 3 with “Fizz”, numbers divisible by 5 with “Buzz”, and numbers divisible by both with “FizzBuzz”.
Multiple Conditions
result = [ "FizzBuzz" if n % 15 == 0 else "Fizz" if n % 3 == 0 else "Buzz" if n % 5 == 0 else n for n in range(1, 21)]print(result)Exercise 3: Nested Comprehension with Conditions
Use nested list comprehension to create a list of lists representing a multiplication table for numbers 1 to 5, but only include products greater than 10.
Nested Comprehension with Conditions
table = [[i * j for j in range(1, 6) if i * j > 10] for i in range(1, 6) if any(i * j > 10 for j in range(1, 6))]print(table)Exercise 4: Dictionary with Conditional Values
Use dictionary comprehension to create a dictionary mapping numbers from 1 to 10, where values are “even” for even numbers and “odd” for odd numbers.
Dictionary with Conditional Values
result = {n: "even" if n % 2 == 0 else "odd" for n in range(1, 11)}print(result)Exercise 5: Complex Nested Structure
Use nested comprehension to create a list of dictionaries, where each dictionary maps numbers 1 to 3 to their squares, but only for even numbers.
Complex Nested Structure
result = [{n: n * n for n in range(1, 4) if n % 2 == 0} for i in range(3)]print(result)