2. Conditional List Comprehension

Quiz: 0/3

Conditional list comprehension allows us to filter and modify elements in a list based on specific conditions, all within a single line of code.

In basic list comprehension, we create lists from an iterable (like a range or another list). But sometimes, we only want to include certain elements that meet specific criteria. This is where conditions come in!

Basic syntax with condition:

[expression for item in iterable if condition]
  • expression → The operation or value you want to add to the list.
  • item → The current element being processed.
  • iterable → The list, range, or any collection you're looping through.
  • condition → A condition that filters which items to include.

Example: Filtering even numbers

Let’s say we want to create a list of only the even numbers from a range of numbers between 1 and 10. We can use conditional list comprehension like this:

even_numbers = [num for num in range(1, 11) if num % 2 == 0]
print(even_numbers)

Explanation:

  • The condition if num % 2 == 0 filters only the even numbers.
  • The num is added to the list only if it passes the condition.

Output:

[2, 4, 6, 8, 10]

Using If-Else in list comprehension

Sometimes, you want to choose between two values depending on a condition. You can combine if-else within list comprehension to do this!

Syntax for If-Else:

[expression if condition else alternative for item in iterable]
  • expression if condition → What to add to the list if the condition is True.
  • else alternative → What to add to the list if the condition is False.

Example: Marking even and odd numbers

Let's say we want to create a list that says whether each number is "even" or "odd" for numbers from 1 to 5.

even_or_odd = ["even" if num % 2 == 0 else "odd" for num in range(1, 6)]
print(even_or_odd)

Explanation:

  • If the number is even, it adds "even" to the list.
  • If the number is odd, it adds "odd" to the list.

Output:

['odd', 'even', 'odd', 'even', 'odd']

Why use conditional list comprehension?

  • It allows you to filter and transform lists in a compact, readable way.
  • It reduces code compared to using traditional loops with if statements.

Hands-On Practise

Example: Filtering odd numbers from a list

Let's create a list of numbers from 1 to 10 and use conditional list comprehension to filter out only the odd numbers.

Output:

Quizzes: 0/3

Question 1:

What does the condition 'if num % 2 != 0' do in list comprehension?

  • It includes only even numbers
  • It includes only odd numbers
  • It includes all numbers
  • It excludes all numbers

Question 2:

What will this list comprehension output: ['Even' if num % 2 == 0 else 'Odd' for num in range(1, 6)]?

  • ['Odd', 'Even', 'Odd', 'Even', 'Odd']
  • ['Even', 'Even', 'Even', 'Even', 'Even']
  • ['Odd', 'Odd', 'Odd', 'Odd', 'Odd']
  • ['Even', 'Odd', 'Even', 'Odd', 'Even']

Question 3:

How can you filter a list to only include numbers that are greater than 10 using list comprehension?

  • [num for num in range(1, 20) if num > 10]
  • [num if num > 10 for num in range(1, 20)]
  • [num if num < 10 else num for num in range(1, 20)]
  • [num for num in range(1, 10) if num > 10]

Feedback

Share your thoughts about this lesson

Discussion

Please log in to create a discussion.
Previous lessonIntroduction to List ComprehensionNext lessonNested Loops and List Comprehension