Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Integralist/1a256fc6e9112a084cddd94a49bb5fba to your computer and use it in GitHub Desktop.
Save Integralist/1a256fc6e9112a084cddd94a49bb5fba to your computer and use it in GitHub Desktop.
Python if/else list comprehension (generator expression)
[i if i is True else 'nope' for i in [True, False, True]]
# [True, 'nope', True]
# Notice this is a conditional expression and different from list comprehension
# Which typically is `for ... if ...`
# Now it's reversed and no expression for truthy condition `if x <condition> else <expression>`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment