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

a = [1, 2, 3, 4, 5] b = [9, 8, 7, 6, 5] returnMatches(a, b)

would return `[5]`

, for instance.

Skip to content
# How can I compare two lists in python and return matches

## The Question :

*406 people think this question is useful*
*The Question Comments :*
## The Answer 1

*530 people think this answer is useful*
## The Answer 2

*428 people think this answer is useful*
## The Answer 3

*115 people think this answer is useful*
## The Answer 4

*72 people think this answer is useful*
## The Answer 5

*20 people think this answer is useful*
## The Answer 6

*17 people think this answer is useful*
## The Answer 7

*15 people think this answer is useful*
## The Answer 8

*12 people think this answer is useful*
## The Answer 9

*7 people think this answer is useful*
## The Answer 10

*6 people think this answer is useful*
## The Answer 11

*5 people think this answer is useful*
## The Answer 12

*4 people think this answer is useful*
## The Answer 13

*4 people think this answer is useful*
## The Answer 14

*3 people think this answer is useful*
## The Answer 15

*2 people think this answer is useful*
## The Answer 16

*2 people think this answer is useful*
## The Answer 17

*1 people think this answer is useful*
## The Answer 18

*0 people think this answer is useful*
## The Answer 19

*0 people think this answer is useful*
### Related Posts

#### node.js – npm – how to show the latest version of a package

#### javascript – jQuery selectors on custom data attributes using HTML5

#### java – Iterate through a HashMap

#### About Author

##### moting1a

2021-01-14

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

a = [1, 2, 3, 4, 5] b = [9, 8, 7, 6, 5] returnMatches(a, b)

would return `[5]`

, for instance.

- The answers below all seem wrong to me. What happens if a number is repeated in either list, surely you’d want to know that (?) (eg., say both lists have ‘5’ twice) Any solution using sets will immediately remove all repeated items and you’ll lose that info.
- Possible duplicate of How to find list intersection?

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

>>> a = [1, 2, 3, 4, 5] >>> b = [9, 8, 7, 6, 5] >>> set(a) & set(b) {5}

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

>>> [i for i, j in zip(a, b) if i == j] [5]

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

Use set.intersection(), it’s fast and readable.

>>> set(a).intersection(b) set([5])

A quick performance test showing Lutz’s solution is the best:

import time def speed_test(func): def wrapper(*args, **kwargs): t1 = time.time() for x in xrange(5000): results = func(*args, **kwargs) t2 = time.time() print '%s took %0.3f ms' % (func.func_name, (t2-t1)*1000.0) return results return wrapper @speed_test def compare_bitwise(x, y): set_x = frozenset(x) set_y = frozenset(y) return set_x & set_y @speed_test def compare_listcomp(x, y): return [i for i, j in zip(x, y) if i == j] @speed_test def compare_intersect(x, y): return frozenset(x).intersection(y) # Comparing short lists a = [1, 2, 3, 4, 5] b = [9, 8, 7, 6, 5] compare_bitwise(a, b) compare_listcomp(a, b) compare_intersect(a, b) # Comparing longer lists import random a = random.sample(xrange(100000), 10000) b = random.sample(xrange(100000), 10000) compare_bitwise(a, b) compare_listcomp(a, b) compare_intersect(a, b)

These are the results on my machine:

# Short list: compare_bitwise took 10.145 ms compare_listcomp took 11.157 ms compare_intersect took 7.461 ms # Long list: compare_bitwise took 11203.709 ms compare_listcomp took 17361.736 ms compare_intersect took 6833.768 ms

Obviously, any artificial performance test should be taken with a grain of salt, but since the `set().intersection()`

answer is *at least as fast* as the other solutions, and also the most readable, it should be the standard solution for this common problem.

I prefer the set based answers, but here’s one that works anyway

[x for x in a if x in b]

Quick way:

list(set(a).intersection(set(b)))

The easiest way to do that is to use sets:

>>> a = [1, 2, 3, 4, 5] >>> b = [9, 8, 7, 6, 5] >>> set(a) & set(b) set([5])

>>> s = ['a','b','c'] >>> f = ['a','b','d','c'] >>> ss= set(s) >>> fs =set(f) >>> print ss.intersection(fs) **set(['a', 'c', 'b'])** >>> print ss.union(fs) **set(['a', 'c', 'b', 'd'])** >>> print ss.union(fs) - ss.intersection(fs) **set(['d'])**

Also you can try this,by keeping common elements in a new list.

new_list = [] for element in a: if element in b: new_list.append(element)

Do you want duplicates? If not maybe you should use sets instead:

>>> set([1, 2, 3, 4, 5]).intersection(set([9, 8, 7, 6, 5])) set([5])

another a bit more functional way to check list equality for list 1 (lst1) and list 2 (lst2) where objects have depth one and which keeps the order is:

all(i == j for i, j in zip(lst1, lst2))

Can use itertools.product too.

>>> common_elements=[] >>> for i in list(itertools.product(a,b)): ... if i[0] == i[1]: ... common_elements.append(i[0])

You can use

def returnMatches(a,b): return list(set(a) & set(b))

You can use:

a = [1, 3, 4, 5, 9, 6, 7, 8] b = [1, 7, 0, 9] same_values = set(a) & set(b) print same_values

Output:

set([1, 7, 9])

a = [1, 2, 3, 4, 5] b = [9, 8, 7, 6, 5] lista =set(a) listb =set(b) print listb.intersection(lista) returnMatches = set(['5']) #output print " ".join(str(return) for return in returnMatches ) # remove the set() 5 #final output

If you want a boolean value:

>>> a = [1, 2, 3, 4, 5] >>> b = [9, 8, 7, 6, 5] >>> set(b) == set(a) & set(b) and set(a) == set(a) & set(b) False >>> a = [3,1,2] >>> b = [1,2,3] >>> set(b) == set(a) & set(b) and set(a) == set(a) & set(b) True

I just used the following and it worked for me:

group1 = [1, 2, 3, 4, 5] group2 = [9, 8, 7, 6, 5] for k in group1: for v in group2: if k == v: print(k)

this would then print 5 in your case. Probably not great performance wise though.

The following solution works for any order of list items and also supports both lists to be different length.

import numpy as np def getMatches(a, b): matches = [] unique_a = np.unique(a) unique_b = np.unique(b) for a in unique_a: for b in unique_b: if a == b: matches.append(a) return matches print(getMatches([1, 2, 3, 4, 5], [9, 8, 7, 6, 5, 9])) # displays [5] print(getMatches([1, 2, 3], [3, 4, 5, 1])) # displays [1, 3]

Using `__and__`

attribute method also works.

>>> a = [1, 2, 3, 4, 5] >>> b = [9, 8, 7, 6, 5] >>> set(a).__and__(set(b)) set([5])

or simply

>>> set([1, 2, 3, 4, 5]).__and__(set([9, 8, 7, 6, 5])) set([5]) >>>

you can | for set union and & for set intersection. for example: set1={1,2,3} set2={3,4,5} print(set1&set2) output=3 set1={1,2,3} set2={3,4,5} print(set1|set2) output=1,2,3,4,5 curly braces in the answer.