Last active
February 21, 2019 08:16
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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