-1

I have two lists filled with integers. I wish to add them together such that:

  1. the output list has no duplicate elements,
  2. is in order, and
  3. contains the union of both lists.

Is there any way to do so without creating my own custom function? If not, what would a neat and tidy procedure look like?

For instance:

list1 = [1, 10, 2]
list2 = [3, 4, 10]

Output:

outputlist = [1, 2, 3, 4, 10]
Adi219
  • 4,712
  • 2
  • 20
  • 43
QuantumPanda
  • 283
  • 3
  • 12

2 Answers2

3

Try this:

combined = [list1, list2]
union = list(set().union(*combined))

This takes advantage of the predefined method (.union()) of set() , which is what you need here.

combined can have as many elements inside it, as the asterisk in *combined means that the union of all of the elements is found.

Also, I list()ed the result but you could leave it as a set().

As @glibdud states in the comments, it's possible that this might produce a sorted list, but it's not guaranteed, so use sorted() to ensure that it's ordered. (like this union = sorted(list(set().union(*combined))))

Adi219
  • 4,712
  • 2
  • 20
  • 43
  • Note that the fact that this produces a sorted result is somewhat accidental. You'll want to do `sorted(set()...)` instead of `list(set()...)` to be sure. (Try adding some larger numbers into the lists.) – glibdud Jun 07 '18 at 20:03
  • if I can nitpick your wording: "It's possible that this might not produce a sorted list" is like saying "it's possible that shuffling a deck of cards won't produce a sorted deck." It's inverse. It's *possible* that `list(some_set)` will produce a sorted list, but there's no reason it should. (also no need to call `list` before `sorted` -- `sorted` returns a list.) – Adam Smith Jun 07 '18 at 20:15
3
l1 = [1, 10, 2]
l2 = [3, 4, 10]

sorted(list(set(l1 + l2)))
>>> [1, 2, 3, 4, 10]
bphi
  • 3,115
  • 3
  • 23
  • 36
  • 2
    Please enlighten me as to the difference between a union without duplicates and a list of unique elements. – bphi Jun 07 '18 at 19:53
  • Note that the fact that this produces a sorted result is somewhat accidental. You'll want to do `sorted(set()...)` instead of `list(set()...)` to be sure. (Try adding some larger numbers into the lists.) – glibdud Jun 07 '18 at 20:05
  • 1
    I wonder if `set(l1+l2)` could be faster than `set().union(l1, l2)`, since the former must first concat the lists, while the latter has essentially no overhead – Adam Smith Jun 07 '18 at 20:12
  • @glibdud I couldn't get it to generate a non-sorted list, although I also couldn't find any documentation regarding this behavior. Do you have a link? – bphi Jun 07 '18 at 20:17
  • 1
    @AdamSmith I think `set().union` is definitely consistently faster, but not substantially faster. This version is quite readable though. – bphi Jun 07 '18 at 20:18
  • 2
    Just adding a couple of 6-7 digit numbers did it for me. I don't have a link, but in general sets are considered unordered. They'll usually maintain order "accidentally" for small integers, but that's an implementation detail and shouldn't be relied upon. – glibdud Jun 07 '18 at 20:19
  • 1
    @AdamSmith This was the first solution that came to mind ... but I was sure that `itertools` would be quicker as it's designed for efficiency. – Adi219 Jun 07 '18 at 20:34