Home > Mobile >  Get duplicates between two lists
Get duplicates between two lists

Time:11-09

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 but I only found how to remove the duplicates.

CodePudding user response:

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.

CodePudding user response:

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 starts 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].

CodePudding user response:

You could use 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))

CodePudding user response:

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.

  • Related