To get the first element found in a list by some condition or None if nothing was found we can use the following construction:
1 2 3 4 |
elements_list = ['One', 'Two', 'Three'] element = next(iter([e for e in elements_list if e in ['One', 'Two']]), None) print(element) # 'One' |
With nothing found it returns None:
1 2 3 |
element = next(iter([e for e in elements_list if e in ['Four', 'Five']]), None) print(element) # None |