Skip to content

Instantly share code, notes, and snippets.

@AlexxIT
Created May 9, 2018 03:37
Show Gist options
  • Save AlexxIT/14db7d1202d61d4505d2bb9ebad43d23 to your computer and use it in GitHub Desktop.
Save AlexxIT/14db7d1202d61d4505d2bb9ebad43d23 to your computer and use it in GitHub Desktop.
In one line in Python
items1 = ['abc', 'bcd', 'cde']
items2 = [[1, 2], [3, 4], [5, 6]]
# return first result from iterator (with optional default)
r1 = next((item for item in items1 if 'd' in item), None)
print(r1)
# return first True result
r2 = any('d' in item for item in items1)
print(r2)
# check all conditions
r3 = all('c' in item for item in items1)
print(r3)
# flatten a list of lists
r4 = [subitem for item in items2 for subitem in item]
print(r4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment