How can I compare two lists in python and return matches

To compare two lists and return the matches, you can use the built-in intersection() method of the set data type. Here's an example:

list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]

matches = list(set(list1).intersection(list2))


This will return the output: [4, 5]

Note that the intersection() method returns a set, so we are converting it to a list to be able to print it. Also you can use & operator for intersection

matches = list(set(list1) & set(list2))

This will also return the same output: [4, 5]