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))

print(matches)

This will return the output: [4, 5]

Watch a course Python - The Practical Guide

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

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

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

print(matches)

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