-4

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?

Matthew Woop
  • 17
  • 1
  • 1
  • 5

2 Answers2

0

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)
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
0

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
lord63. j
  • 4,500
  • 2
  • 22
  • 30