if else in a list comprehension

PythonListList ComprehensionIf Statement

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

>>> 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.

Solution 2 - Python

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

Solution 3 - Python

[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] >

Solution 4 - Python

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]

Solution 5 - Python

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))

Solution 6 - Python

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

Solution 7 - Python

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 ] 

Solution 8 - Python

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).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser225312View Question on Stackoverflow
Solution 1 - Pythonuser225312View Answer on Stackoverflow
Solution 2 - Pythonarboc7View Answer on Stackoverflow
Solution 3 - PythonDan D.View Answer on Stackoverflow
Solution 4 - PythonAndiDogView Answer on Stackoverflow
Solution 5 - PythonXiaojun ChenView Answer on Stackoverflow
Solution 6 - PythonStefan GruenwaldView Answer on Stackoverflow
Solution 7 - PythonszeitlinView Answer on Stackoverflow
Solution 8 - PythonJeetView Answer on Stackoverflow