When the Difference Between Using "if" and "if-else" in a Python List Comprehension?

Python List Comprehension if vs if-else – What's the Difference?

In a list comprehension, the if and if-else clauses are different in terms of syntax, purpose and placement:

Difference if if-else
Syntax: Part of the list comprehension syntax It is the ternary operator (which is a language construct, and not a part of list comprehension syntax)
Purpose: Filtering items Transforming items
Placement: Appears after for clause(s) Appears before main for clause (in the expression part of the list comprehension)

Syntax

The if clause is a part of the list comprehension syntax, which has the following basic syntax:

[expression for item in iterable if condition]

Where:

  • expression is the value that's added to the resulting list only if the condition is True;
  • item is a variable that takes on the values from the iterable one at a time, and;
  • condition is a boolean expression that is evaluated for each item.

You can compare this to a list comprehension with an if-else clause, which has the following basic syntax:

[expression_1 if condition else expression_2 for item in iterable]

The if-else clause is not a part of the list comprehension syntax, and is in fact the ternary operator (which is a language construct). It can be used to add a conditional expression to a list comprehension, where:

  • expression_1 is the value that's added in the resulting list for items that satisfy the condition;
  • expression_2 is the value that's added in the resulting list for items that do not satisfy the condition;
  • item is a variable that takes on the values from the iterable one at a time, and;
  • condition is a boolean expression that is evaluated for each item.

Purpose

The if clause is commonly used for filtering the items in the list that the comprehension generates.

Consider, for example, the following where the if clause is used in a list comprehension to filter out all odd numbers, returning only the even numbers:

nums = [1, 2, 3, 4, 5, 6]
filtered_nums = [n for n in nums if n % 2 == 0]

print(filtered_nums) # [2, 4, 6]

You can compare this to a list comprehension with an if-else clause, which is commonly used for transforming the items (to one value or the other) in the list that the comprehension generates.

Consider, for example, the following conditional expression that's used in a list comprehension to conditionally transform even/odd numbers into 'even' or 'odd' strings:

nums = [1, 2, 3, 4, 5, 6]
filtered_nums = ['even' if n % 2 == 0 else 'odd' for n in nums]

print(filtered_nums) # ['odd', 'even', 'odd', 'even', 'odd', 'even']

Placement

The if clause is placed after for clause(s):

[expression for item in iterable if condition]
[expression for item in iterable if condition_1 ... if condition_n]
[expression for item_1 in iterable_1 if condition_1 ... for item_n in iterable_n if condition_n]

There can be zero or more for or if clauses in a comprehension syntax.

It's important to keep in mind that the order of if clauses matter, as each clause is evaluated in the order they appear, and the items that don't pass the condition of a clause will not be passed to next clause.

In contrast, the if-else clause is used in the expression part of the list comprehension:

[expression_1 if condition else expression_2 for item in iterable]
[expression_1 if condition_1 else expression_2 ... if condition_n else expression_n+1 for item in iterable]

There can be zero or more if-else conditional expressions present in a list comprehension.

Although, it's possible to use multiple if-else in a list comprehension, you should avoid it as it leads to hard-to-read code.

It's important to keep in mind that the order of the if-else clauses matter, as each clause is evaluated in the order they appear. It's read from left-to-right, and the first clause whose condition is True is evaluated and corresponding expression is returned. Therefore, the order of conditions may affect the result.


Hope you found this post useful. It was published . Please show your love and support by sharing this post.