Skip to content
Advertisement

How can I compare two lists in python and return matches [duplicate]

I want to take two lists and find the values that appear in both.

JavaScript

would return [5], for instance.

Advertisement

Answer

Not the most efficient one, but by far the most obvious way to do it is:

JavaScript

if order is significant you can do it with list comprehensions like this:

JavaScript

(only works for equal-sized lists, which order-significance implies).

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement