python – if/else in a list comprehension

python – if/else in a list comprehension

You can totally do that. Its just an ordering issue:

[unicode(x.strip()) if x is not None else  for x in row]

In general,

[f(x) if condition else g(x) for x in sequence]

And, for list comprehensions with if conditions only,

[f(x) for x in sequence if condition]

Note that this actually uses a different language construct, a conditional expression, which itself is not part of the comprehension syntax, while the if after the for…in is part of list comprehensions and used to filter elements from the source iterable.


Conditional expressions can be used in all kinds of situations where you want to choose between two expression values based on some condition. This does the same as the ternary operator ?: that exists in other languages. For example:

value = 123
print(value, is, even if value % 2 == 0 else odd)

The specific problem has already been solved in previous answers, so I will address the general idea of using conditionals inside list comprehensions.

Here is an example that shows how conditionals can be written inside a list comprehension:

X = [1.5, 2.3, 4.4, 5.4, n, 1.5, 5.1, a]     # Original list

# Extract non-strings from X to new list
X_non_str = [el for el in X if not isinstance(el, str)]  # When using only if, put for in the beginning

# Change all strings in X to b, preserve everything else as is
X_str_changed = [b if isinstance(el, str) else el for el in X]  # When using if and else, put for in the end

Note that in the first list comprehension for X_non_str, the order is:

expression for item in iterable if condition

and in the last list comprehension for X_str_changed, the order is:

expression1 if condition else expression2 for item in iterable

I always find it hard to remember that expression1 has to be before if and expression2 has to be after else. My head wants both to be either before or after.

I guess it is designed like that because it resembles normal language, e.g. I want to stay inside if it rains, else I want to go outside

In plain English the two types of list comprehensions mentioned above could be stated as:

With only if:

extract_apple for apple in apple_box if apple_is_ripe

and with if/else

mark_apple if apple_is_ripe else leave_it_unmarked for apple in apple_box

python – if/else in a list comprehension

python – if/else in a list comprehension

One way:

def change(f):
    if f is None:
        return unicode(f.strip())
    else:
        return 

row = [change(x) for x in row]

Although then you have:

row = map(change, row)

Or you can use a lambda inline.

Leave a Reply

Your email address will not be published.