list_a = [1, 2, 3, 4, 5, 6]
list_b = [8, 9, 5, 10]
check = any(item in list_a for item in list_b)
This is what I'm doing to check if there are elements from list_A in list_b but I want to know which exactly are these elements.
list_a = [1, 2, 3, 4, 5, 6]
list_b = [8, 9, 5, 10]
check = any(item in list_a for item in list_b)
This is what I'm doing to check if there are elements from list_A in list_b but I want to know which exactly are these elements.
You can covert them to set
and try the below code:
list_a = set([1, 2, 3, 4, 5, 6])
list_b = set([8, 9, 5, 10])
check = list_a.intersection(list_b)
print(check)
maybe you can use set(), but i don't know the requirements about your code:
math_items = list(set(list_a) & set(list_b))
You have to keep in mind that set() eliminates the repeated elements in the list leaving only one, I don't know if this works for you, since I don't know if you should also count how many times that element appears in the other list.
Caveat: This approach is not a good idea for big lists but it's easy to understand.
list_a = [1, 2, 3, 4, 5, 6]
list_b = [8, 9, 5, 10]
for firstItem in list_a: # Take each item in the first list,
for secondItem in list_b: # and check it against each item in the second list.
if firstItem == secondItem: # If the items match,
print(firstItem, 'is in both lists') # then print the item.