Skip to content

Instantly share code, notes, and snippets.

@matthewfeickert
Created April 27, 2021 16:06
Show Gist options
  • Save matthewfeickert/00f11bc92858314c9ddff83a7d95f6a2 to your computer and use it in GitHub Desktop.
Save matthewfeickert/00f11bc92858314c9ddff83a7d95f6a2 to your computer and use it in GitHub Desktop.
Python tricks and patterns
# Use the generator .next method to quickly get the first non-None item in a list
if __name__ == "__main__":
item_list = [None, None, "non-None-A", "non-None-B", None]
item = next(item for item in item_list if item is not None)
assert item == "non-None-A"
print(item)
# - axes = next(
# - artist
# - for artist in [self.cls_obs, self.clsb, self.clb]
# - if artist is not None
# - ).axes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment