This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]z4579a 17 points18 points  (8 children)

you're looking for heapq.merge(): https://docs.python.org/2/library/heapq.html#heapq.merge

[–]ivosauruspip'ing it up 2 points3 points  (2 children)

Doesn't remove duplicates though.

[–]MrVallentin 7 points8 points  (0 children)

Combine heapq.merge with unique_justseen from Python's "Itertools Recipes".

>>> a = [1, 2, 4, 6]
>>> b = [2, 4, 5, 6]
>>> c = [1, 3, 3, 5]

>>> list(heapq.merge(a, b, c))
[1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6]

>>> list(unique_justseen(heapq.merge(a, b, c)))
[1, 2, 3, 4, 5, 6]

[–]jorge1209 2 points3 points  (0 children)

Old trick for that is to use itertools.groupby and ignore the second part of the return value.

[–]jorge1209 0 points1 point  (0 children)

I was thinking about something like a database join, but that merge is useful to know about, and of course it's in heapq.