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.

Post History

83%
+8 −0
Q&A Why are list comprehensions written differently if you use `else`?

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

posted 1y ago by Karl Knechtel‭  ·  edited 12mo ago by Karl Knechtel‭

Answer
#2: Post edited by user avatar Karl Knechtel‭ · 2023-12-01T23:16:54Z (12 months ago)
Update link to refer to new canonical I wrote for the syntax in question
  • ## These two uses of `if` are different
  • The `if` at the end of a list comprehension syntax:
  • ```python
  • [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:
  • ```python
  • [num if num != 11 else 22 for num in hand]
  • ```
  • is part of a *conditional [expression](https://software.codidact.com/posts/289228)* `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.
  • ## These two uses of `if` are different
  • The `if` at the end of a list comprehension syntax:
  • ```python
  • [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:
  • ```python
  • [num if num != 11 else 22 for num in hand]
  • ```
  • is part of a *[conditional expression](https://software.codidact.com/posts/290335)* `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.
#1: Initial revision by user avatar Karl Knechtel‭ · 2023-10-19T21:09:40Z (about 1 year ago)
## These two uses of `if` are different

The `if` at the end of a list comprehension syntax:
```python
[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:
```python
[num if num != 11 else 22 for num in hand]
```
is part of a *conditional [expression](https://software.codidact.com/posts/289228)* `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.