if else in a list comprehension

Question:

I have a list l:

l = [22, 13, 45, 50, 98, 69, 43, 44, 1]

For numbers above 45 inclusive, I would like to add 1; and for numbers less than it, 5.

I tried

[x+1 for x in l if x >= 45 else x+5]

But it gives me a syntax error. How can I achieve an ifelse like this in a list comprehension?

Asked By: user225312

||

Answers:

You must put the expression at the beginning of the list comprehension, an if statement at the end filters elements!

[x+1 if x >= 45 else x+5 for x in l]
Answered By: AndiDog
>>> l = [22, 13, 45, 50, 98, 69, 43, 44, 1]
>>> [x+1 if x >= 45 else x+5 for x in l]
[27, 18, 46, 51, 99, 70, 48, 49, 6]

Do-something if <condition>, else do-something else.

Answered By: user225312
[x+1 if x >= 45 else x+5 for x in l]

And for a reward, here is the comment, I wrote to remember this the first time I did this error:

Python’s conditional expression is a if C else b and can’t be used as:

[a for i in items if C else b]

The right form is:

[a if C else b for i in items]

Even though there is a valid form:

[a for i in items if C]

But that isn’t the same as that is how you filter by C, but they can be combined:

[a if tC else b for i in items if fC]
Answered By: Dan D.

You could move the conditional to:

v = [22, 13, 45, 50, 98, 69, 43, 44, 1]
[ (x+1 if x >=45 else x+5)  for x in v ]

But it’s starting to look a little ugly, so you might be better off using a normal loop. Note that I used v instead of l for the list variable to reduce confusion with the number 1 (I think l and O should be avoided as variable names under any circumstances, even in quick-and-dirty example code).

Answered By: Jeet

The reason you’re getting this error has to do with how the list comprehension is performed.

Keep in mind the following:

[ expression for item in list if conditional ]

Is equivalent to:

for item in list:
    if conditional:
        expression

Where the expression is in a slightly different format (think switching the subject and verb order in a sentence).

Therefore, your code [x+1 for x in l if x >= 45] does this:

for x in l:
    if x >= 45:
        x+1

However, this code [x+1 if x >= 45 else x+5 for x in l] does this (after rearranging the expression):

for x in l:
    if x>=45: x+1
    else: x+5
Answered By: arboc7

You can also put the conditional expression in brackets inside the list comprehension:

    l = [22, 13, 45, 50, 98, 69, 43, 44, 1]
    print [[x+5,x+1][x >= 45] for x in l]

[false,true][condition] is the syntax

Answered By: Stefan Gruenwald

I just had a similar problem, and found this question and the answers really useful. Here’s the part I was confused about. I’m writing it explicitly because no one actually stated it simply in English:

The iteration goes at the end.

Normally, a loop goes

for this many times:
    if conditional: 
        do this thing
    else:
        do something else  

Everyone states the list comprehension part simply as the first answer did,

[ expression for item in list if conditional ] 

but that’s actually not what you do in this case. (I was trying to do it that way)

In this case, it’s more like this:

[ expression if conditional else other thing for this many times ] 
Answered By: szeitlin

Like in [a if condition1 else b for i in list1 if condition2], the two ifs with condition1 and condition2 doing two different things. The part (a if condition1 else b) is from a lambda expression:

lambda x: a if condition1 else b

while the other condition2 is another lambda:

lambda x: condition2

Whole list comprehension can be regard as combination of map and filter:

map(lambda x: a if condition1 else b, filter(lambda x: condition2, list1))
Answered By: Xiaojun Chen