W3docs

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.

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:

Compare two lists and return the matches in Python by the itersection method

python— editable, runs on the server

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

Compare two lists and return the matches in Python by the & symbol

python— editable, runs on the server

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