Created
May 9, 2018 03:37
-
-
Save AlexxIT/14db7d1202d61d4505d2bb9ebad43d23 to your computer and use it in GitHub Desktop.
In one line in Python
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
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