2

I'm looking to see if item is in list of list.

List1 = [['a','b','c'],['d','e','f'],['g','h','i']]

If b is in any of the list in a list, then print True. Else, print False.

What's the pythonic way to do this?

kaktus_car
  • 986
  • 2
  • 11
  • 19

2 Answers2

9

Try this:

# check element exists in list of list or not? 
result = any("b" in sublist for sublist in List1) 

# printing result 
print(str(result))
Jay
  • 149
  • 7
0

I'll play the fool...

>>> 'b' in sum(List1, [])
True
>>> 'B' in sum(List1, [])
False
superb rain
  • 5,300
  • 2
  • 11
  • 25