Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on Why are list comprehensions written differently if you use `else`?

Parent

Why are list comprehensions written differently if you use `else`?

+14
−0

The following list comprehension worked when I tried it:

[num for num in hand if num != 11]

But this doesn't work:

[num for num in hand if num != 11 else 22]

It gives a SyntaxError, highlighting the else.

This led me to believe that you can't use else in a list comprehension. However, I then discovered that this is possible instead:

[num if num != 11 else 22 for num in hand]

Why does the if need to be placed earlier in the comprehension in order to include a matching else?

History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

Post
+7
−0

These two uses of if are different

The if at the end of a list comprehension syntax:

[num for num in hand if num != 11]

is a filter; its purpose is to decide whether or not the resulting list should contain a value that corresponds to any given num (for each one found in hand).

This is part of the list comprehension syntax. It cannot have a corresponding else, because that doesn't make logical sense: the purpose is to exclude elements when the condition isn't met, so specifying an alternate value wouldn't be meaningful.

The if in the working if-else example:

[num if num != 11 else 22 for num in hand]

is part of a conditional expression num if num != 11 else 22; it is not part of the list comprehension syntax, and it independent meaning. This expression evaluates to num when num != 11, and to 22 otherwise (i.e., when num == 11).

Such an expression must contain an else part, because it's part of the syntax. It's a conditional expression - not a condition that controls execution of the prior code. Python doesn't have a "void type", and Python expressions must evaluate to some value (or raise an exception) regardless of the input; so a resulting value needs to be specified for both cases of the conditional.

History
Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

Meta observations (1 comment)
Meta observations
Karl Knechtel‭ wrote 6 months ago

I decided to add an answer to this old question in order to try to expand some detail and give more clarity, taking into account the comment feedback on the original answer. That answer is high-quality and very much to the point; but some readers might want a more in-depth treatment.

At some future point I expect we'll have a canonical about the "ternary operator" (Python's if-else conditional expression), and then I'll edit in a link from here to there.