I have
list_of_lists = [ ['a', 'b', 'c'] , ['b','c'] , ['c'] ]
and I want to determine whether or not
'd'
is a member of my list of lists. How do I go about doing that in Python?
I have
list_of_lists = [ ['a', 'b', 'c'] , ['b','c'] , ['c'] ]
and I want to determine whether or not
'd'
is a member of my list of lists. How do I go about doing that in Python?
The precise answer to your question (whether 'd' is a member of my list of lists) is
'd' in list_of_lists
But I suspect your question is just poorly worded
If your question was whether 'd' is a member of one of the items of list_of_lists, then the answer is
any('d' in lst for lst in list_of_lists)
we can define a function, so it can handle arbitrary nested list.
def flatten(container):
for item in container:
if isinstance(item, list):
for inner_item in flatten(item):
yield inner_item
else:
yield item
>>> 'd' in flatten([['a', 'b', 'c'], ['b','c'], ['c']])
False
>>> 'd' in flatten([['a', 'b', 'c'], ['b','c', ['d']], ['c']])
True