How to take two lists and combine them, excluding any duplicates?

I would like to make one list of two separate lists of unique elements.

There are other similar questions, but there didnโ€™t seem to be a problem with the effectiveness of this problem, since the lists are several million items.

Completely unrelated: am I the only one who hates how the tag suggestion window closes the "send your question" button?

+12
python
Jan 12 '11 at 21:00
source share
4 answers

Use set .

 >>> first = [1, 2, 3, 4] >>> second = [3, 2, 5, 6, 7] >>> third = list(set(first) | set(second)) # '|' is union >>> third [1, 2, 3, 4, 5, 6, 7] 
+31
Jan 12 2018-11-11T00:
source share

A slightly more efficient way to do this:

 >>> first = [1, 2, 3, 4] >>> second = [3, 2, 5, 6, 7] # New way >>> list(set(first + second)) [1, 2, 3, 4, 5, 6, 7] #1000000 loops, best of 3: 1.42 ยตs per loop # Old way >>> list(set(first) | set(second)) [1, 2, 3, 4, 5, 6, 7] #1000000 loops, best of 3: 1.83 ยตs per loop 

The new method is more efficient since it has only one set () instead of 2.

+10
Jan 13 '16 at 9:16
source share
 >>> l1 = range(10) >>> l2 = range(5, 15) >>> set(l1) | set(l2) set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]) 
+6
Jan 12 2018-11-11T00:
source share

If someone wants to do this without set() :

 a = [1,2,3] b = [2,3,4] newlist=[] for i in a: newlist.append(i) for z in b: if z not in newlist: newlist.append(z) newlist.sort() print newlist 
+2
Sep 01 '15 at 0:02
source share



All Articles