+ 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 Respuestas
+ 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']])]