Search for Multiple Sublists of Same List in Python
everyone!
I need my Python code to search all the sublists of the given list, but
this does not work when I'm searching for an element contained in only one
of them. For example, this is my code:
data = [[4,5],[4,7]]
search = 4
for sublist in data:
if search in sublist:
print("there", sublist)
else:
print("not there")
print(data)
and this works very well if my search is contained in all of the sublists
of the lists. However, if my search is, for instance, 5, then I get:
there [4,5] #I only want this part.
not there # I don't want the rest.
[[4, 5], [4, 7]]
Thanks, 5813.
No comments:
Post a Comment