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 if – else like this in a list comprehension?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
>>> 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.
Method 2
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
Method 3
[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 band 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]
Method 4
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]
Method 5
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))
Method 6
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
Method 7
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 ]
Method 8
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).
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0