Skip to content

Instantly share code, notes, and snippets.

@hellpanderrr
Last active February 21, 2019 08:16
Show Gist options
  • Save hellpanderrr/70a516d807f909f181d0606ef86ff402 to your computer and use it in GitHub Desktop.
Save hellpanderrr/70a516d807f909f181d0606ef86ff402 to your computer and use it in GitHub Desktop.
python unpack list with 0 or 1 elements, as an expression (without exceptions)
#e.g. if you have a list comprehension that can return only 1 element or an empty list
>>> reduce(lambda x,y:y, [], None)
None
>>> reduce(lambda x,y:y, [5], None)
5
>>> return [5] if [5] else None
5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment