4

I'm sure this question has come up before, but I couldn't find an exact example.

I have 2 lists and want to append the second to the first, only of the values are not already there.

So far I have working code, but was wondering if there were a better, more "Pythonic" was of doing this:

>>> list1
[1, 2, 3]
>>> list2
[2, 4]
>>> list1.extend([x for x in list2 if x not in list1])
>>> list1
[1, 2, 3, 4]

EDIT Based on comments made, this code does not satisfy adding only once, ie:

>>> list1 = [1,2,3]
>>> list2 = [2,4,4,4]
>>> list1.extend([x for x in list2 if x not in list1])
>>> list1
[1, 2, 3, 4, 4, 4]

How would I end up with only:

[1, 2, 3, 4]
port5432
  • 5,889
  • 10
  • 60
  • 97

2 Answers2

5

If you want to maintain the order, you can use collections.OrderedDict like this

from collections import OrderedDict
from itertools import chain
list1, list2 = [1, 2, 3], [2, 4]
print list(OrderedDict.fromkeys(chain(list1, list2)))
# [1, 2, 3, 4]

If the order of the elements is not important, you can use the set like this

from itertools import chain
list1, list2 = [1, 2, 3], [2, 4]
print list(set(chain(list1, list2)))
# [1, 2, 3, 4]
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
4

A way could be using built in type set:

list(set(list1).union(list2))

You would need to store the result of the operation, if you wanted to extend list1 then you can assign it to list1:

list1=list(set(list1).union(list2))

Note: Keep in mind that this approach may not keep the order of the elements in the list.

Hope this helps!

Paulo Bu
  • 29,294
  • 6
  • 74
  • 73
  • 2
    `list(set(list1).union(list2))` would avoid building an intermediary list object. – Martijn Pieters Mar 06 '14 at 13:40
  • @MartijnPieters Is there a different between the two? I mean, I know the difference but is it relevant? – Paulo Bu Mar 06 '14 at 13:41
  • Both this answer and @thefourtheye 's answer work. I would go for this one as it doesn't require an additional import. – port5432 Mar 06 '14 at 13:47
  • 1
    The actual answer is: list1 = list(set(list1).union(list2)) – port5432 Mar 06 '14 at 13:48
  • @ardochhigh Seems so, since the user was aiming for `extend` – Paulo Bu Mar 06 '14 at 13:49
  • 1
    @PauloBu: `set(list1 + list2)` builds an intermediary list object, `set(list1).union(list2)` builds an intermediary set object, but it'll be smaller. – Martijn Pieters Mar 06 '14 at 13:49
  • 2
    @ardochhigh before ticking the answer, please check if the output of this answer is good for you, if the input is `list1, list2 = [100, 200, 300], [200, 400]` – thefourtheye Mar 06 '14 at 13:50
  • @thefourtheye I made a note about it in the answer as well just to prevent the OP. – Paulo Bu Mar 06 '14 at 13:51
  • @thefourtheye I see the list is now out of sequence, and that is worth thinking about. Order is not important in this case though (it is a list of database ids) – port5432 Mar 06 '14 at 13:53
  • 1
    @ardochhigh Oh then okay :) Then this is the best solution you can get :) If the order matters, check my answer. – thefourtheye Mar 06 '14 at 13:54