7
Lesson 7
Ternary (if-else) expressions in list comprehensions
A ternary expression in Python is a short way to write an if-else statement in a single line. When used in a list comprehension, it allows you to add conditional logic for each item in the list while creating a new list.
Here’s the general format of a ternary expression:
Basic structure of a ternary expression
Here’s the general format of a ternary expression:
value_if_true if condition else value_if_false
This reads like:
- If the condition is true, use
value_if_true . - Otherwise, use
value_if_false .
Using ternary expressions in list comprehensions
In a list comprehension, the ternary expression decides what value to include in the new list for each item.
Basic Structure:
[do_this_if_true if condition else do_this_if_false for item in iterable]
Example 1: Replace Negative Numbers with Zero
Let’s transform a list of numbers so that:
- Positive numbers stay the same.
- Negative numbers are replaced with
0 .
Without list comprehension:
numbers = [-1, 2, -3, 4] new_numbers = [] for num in numbers: if num >= 0: new_numbers.append(num) else: new_numbers.append(0) print(new_numbers) # Output: [0, 2, 0, 4]
With a ternary expression in a list comprehension:
numbers = [-1, 2, -3, 4] new_numbers = [num if num >= 0 else 0 for num in numbers] print(new_numbers) # Output: [0, 2, 0, 4]
How it works:
1. For each
- If
num >= 0 , includenum in the new list. - Otherwise, include
0 .
Example 2: Mark even and odd numbers
Let’s create a list of strings that says "Even" or "Odd" for each number.
numbers = [1, 2, 3, 4] labels = ["Even" if num % 2 == 0 else "Odd" for num in numbers] print(labels) # Output: ['Odd', 'Even', 'Odd', 'Even']
How it works:
1. For each
- If
num % 2 == 0 , the condition is true, so include"Even" . - Otherwise, include
"Odd" .
Example 3: Convert grades to Pass/Fail
You can use a ternary expression to label grades as "Pass" or "Fail" based on a passing mark of
grades = [45, 67, 89, 32, 50] status = ["Pass" if grade >= 50 else "Fail" for grade in grades] print(status) # Output: ['Fail', 'Pass', 'Pass', 'Fail', 'Pass']
Breaking It down
Let’s rewrite the above comprehension for clarity:
1. Loop:
2. Condition:
3. Result:
"Pass" if the condition is true."Fail" otherwise.
It’s the same as this:
status = [] for grade in grades: if grade >= 50: status.append("Pass") else: status.append("Fail")
Ternary expressions vs. Conditional filters
Be careful not to confuse ternary expressions with filters in list comprehensions.
- Ternary expressions produce a value for every item, regardless of whether the condition is true or false.
- Filters (using
if only) include items in the list only if the condition is true.
Example: Filter positive numbers
numbers = [-1, 2, -3, 4] positives = [num for num in numbers if num >= 0] print(positives) # Output: [2, 4]
Here, only the numbers that meet the condition
Key Tips
1. Understand the syntax: Always write the true case first, then the condition, and finally the false case.
Example:
2. Use ternary expressions when you want a result for every item.
3. For simple cases, try both regular loops and comprehensions to understand the flow.