I have this code:
table is a list.
if 'ping' in table or 'pong' in table:
# Do something here
Is there a shorter way to write this? I don't want to have table duplicated in the if statement.
I have this code:
table is a list.
if 'ping' in table or 'pong' in table:
# Do something here
Is there a shorter way to write this? I don't want to have table duplicated in the if statement.
You can use set.intersection
:
if {'ping', 'pong'}.intersection(table):
# Do something here
You can use map
:
>>> table = 'pong'
>>> if any(map(lambda pattern: pattern in table, ['ping', 'pong'])):
... print('found!')
...
found!
or:
>>> table = 'pong'
>>> if any(pattern in table for pattern in ['ping', 'pong'])):
... print('found!')
...
found!