You have a list of tuples and a string, and you want to do_something()
every time the string appears in the second part of a tuple.
def someMethod(some_list, some_string):
[do_something() for _, s in some_list if s == some_string]
The trick is to get at the second part of the tuple by unpacking it into _, s
, where _
is a throwaway variable.
Unfortunately you can't execute the statement print 'do_something'
inside a list comprehension. However, you can call the print function, which will need to be explicitly imported if you're using Python 2:
from __future__ import print_function
def someMethod(some_list, some_string):
[print('do_something') for _, s in some_list if s == some_string]