list1 = ['bd 100bd has 15', 'bd 100abd has 0', 'bd 100bbd has 10', 'bd 100cbd has 0', 'bd 100dbd has 0']
list2 = ['100', '100a']
new_list = []
idx =0
for item in list1:
if any(x in line for x in list2):
new_list.insert(list1)
idx = 1
print(new_list)
I am trying to print a new list that will compare the two lists and if list1
has the string from list2
(i.e 100) it will print the string.
new_list = ['bd 100bd has 15', 'bd 100abd has 0']
CodePudding user response:
result = []
for item_1 in list_1:
for item_2 in list_2:
if item_2 in item_1:
if item_1 not in result:
result.append(item_1)
print(len(result))
CodePudding user response:
If you are looking at if a string is EQUAL to a string from list1, this should work. If you are looking for if it contains it, then your solution should work.
for item in list1:
if (item in list2 or item in list2):
new_list.append(item)
idx = 1
If you have more elements in list1, use a nested for loop and check for each individual element in list2