In this case, instead of checking that there is 'abc' in the list (if 'abc' in my_list
), you should check that there is 'abc' in each of the list items.
some_list = ['abc-123', 'def-456', 'ghi-789', 'abc-456']
#If you write it long,
for s in some_list:
if "abc" in s:
print ("Yes")
Break; # If I find one, I won't find the back
#If you write it short,
if any("abc" in s for s in some_list):
print ("Yes")
some_list = ['abc-123', 'def-456', 'ghi-789', 'abc-456']
#If you write it long,
resultlist = []
for s in some_list:
if "abc" in s:
resultlist.append(s)
#If you write it short,
matching = [s for s in some_list if "abc" in s]
© 2024 OneMinuteCode. All rights reserved.