+ 1
Searching in lists in a list python3
Hey, I have lists in a list for example: List = [[âa,âbâ],[âfed,âFrâ]] and I like to check whether âfedâ is in any of those list. Furthermore iâd like to do this in one line. My solution so far: if True in ([âfedâ in i for i in List]) Any better solutions?
8 Answers
+ 3
I would do this:
if any('fed' in sublist for sublist in List):
...
+ 4
clemens, 'return' can only be used from within functions.
+ 3
[Updated] You. an also use 2 for loops for that:
lst = [["a","b"],["fed","Fr"]]
for i in lst:
for j in i:
if "fed" in j:
res = True if "fed" in j else False
print(res)
+ 3
Code is updated to show True or False.
+ 2
Thanks for the answer. is there anyway, that i just get a True or False as a reasult?
+ 2
why does something like this dont work?
[return True if 'fed' in x for i,x in enumerate([['a','b'], [ 'fed','fr']])]