Skip to content

Instantly share code, notes, and snippets.

@GuilhermeHideki
Created April 27, 2016 07:31
Show Gist options
  • Save GuilhermeHideki/17c7a433943da4aacc90c49229b1835d to your computer and use it in GitHub Desktop.
Save GuilhermeHideki/17c7a433943da4aacc90c49229b1835d to your computer and use it in GitHub Desktop.
Python coalesce
def coalesce(*args):
"""Returns the first not None item. Similar to ?? in C#.
It's different from a or b, where false values are invalid.
:param args: list of items for checking
:return the first not None item, or None
"""
return next((arg for arg in args if arg is not None))
@SyraD
Copy link

SyraD commented Nov 26, 2018

Found your snip helpful.

Here is a slight mod I'm using: https://gist.github.com/SyraD/632cdbdf9cf52d77ecfb352bd697acb2/revisions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment