0
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.

Val
  • 1

3 Answers3

0

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)
Sam Dani
  • 114
  • 1
  • 6
0

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.

Jose Moreno
  • 104
  • 4
-1

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.
Andrew H
  • 466
  • 10
  • 22
  • I'm not sure why this was downvoted. It clearly and simply answers the OP's question, it does not require a type conversion and runs in one third of the time of the code above. – Andrew H Jun 29 '21 at 06:03