diacidos
diacidos

Reputation: 11

Get duplicates between two lists

I want to get the duplicates between two lists. Something like this:

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

duplicates = getDuplicates(list1, list2)
print(duplicates)  # =>  = [1,2,4]

I tried to search for an answer, but I only found how to remove the duplicates.

Upvotes: 0

Views: 1403

Answers (4)

jsbueno
jsbueno

Reputation: 110166

By converting one of the lists to a set prior to picking members, this operation will run in linear time - rather than in quadratic time.

That can make a huge difference if both lists start to get to the hundreds of thousands of items:

set1 = set(list1)
duplicates = [item for item in list2 if item in set1]

otherwise, for just a handful of items, a simple filter is enough:

duplicates = [item for item in list2 if item in list1]

Upvotes: 0

mrconcerned
mrconcerned

Reputation: 1935

You could use a loop, but I think it is slow in bigger arrays. In that case, it should be

duplicates = [i for i in list1  if i in list2]

or you can use sets

set(list1) & set(list2)

or you can use the intersection which is faster.

duplicates = set(list1).intersection(set(list2))

Upvotes: 0

Michael M.
Michael M.

Reputation: 11070

You can use a set and the .intersection() method. Like this:

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

duplicates = list(set(list1).intersection(list2))
print(duplicates)  # => [1, 2, 4]

I tested this against jsbueno's answer using timeit and found that my answer is significantly faster. For two lists of 5 elements each, my answer took 0.64 seconds for 1,000,000 runs, while jsbueno's took 0.74 seconds. When scaled up to two lists of 100 elements, my answer took only 4.54 seconds while his answer took 8.08. My takeaway from this is that the built-in set.intersection scales much better than a list comprehension.

Upvotes: 1

Germeloper
Germeloper

Reputation: 71

You can do this to get the duplicates.

list1 = [1, 2, 3, 4, 5]
list2 = [1, 2, 8, 4, 6]
print (set(list1) & set(list2))

This only works for equal sized lists, does implies significance order, or you can do the other way below which does work for unequal sized lists.

list1 = [1, 2, 3, 4, 5]
list2 = [1, 2, 8, 4, 6]
print (set(list1).intersection(list2))

Hope it helps.

Upvotes: 2

Related Questions