1

I have two ordered lists without duplicates. I need to create a third one in the most efficient way possible but I'm stuck. I could do it like this:

list1.extend(list2)
list1.sort(key=lambda x: x.id)

but I don't think this is efficient at all considering that both of the lists are already ordered. Do you have any suggestions on how to do it efficently?

Example:

list1 = [[40, 1980], [50, 1970], [70, 1980], [90, 1975]]
list2 = [[60, 1980],[65,1985]]


list3 = [[40, 1980],[50, 1970],[60, 1980],[65, 1985],[70, 1980],[90, 1975]]

PS: to the order the only thing that matters is the first variable which is 'id'

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Alberto
  • 11
  • 2
  • 2
    It's actually quite efficient, and probably one of the cleanest ways to do it. – PApostol Jan 19 '22 at 19:42
  • Python uses the [Timsort](https://en.wikipedia.org/wiki/Timsort) sorting algorithm which is optimized for sorted and nearly-sorted inputs. It will detect and merge the two sorted runs in a single pass. – John Kugelman Jan 19 '22 at 19:43
  • https://stackoverflow.com/a/465043/9946982 check this – Rajat Tanwar Jan 19 '22 at 19:43
  • and besides that, unless you have _new_ data in both lists a lot of time, and have to repeat it a lot (say, when processing a network message), the time and resources for that are negligible compared with your overall program anyway. But the algorithm for that might be fun. – jsbueno Jan 19 '22 at 19:44

2 Answers2

1

Python has a module in the standard library called heapq. It deals with sorted priority queues and min/max heaps.

heapq.merge allows you to effectively merge multiple sorted inputs:

>>> import operator, heapq
>>> list1 = [[40, 1980], [50, 1970], [70, 1980], [90, 1975]]
>>> list2 = [[60, 1980],[65,1985]]
>>> list(heapq.merge(list1, list2, key=operator.itemgetter(0))
[[40, 1980], [50, 1970], [60, 1980], [65, 1985], [70, 1980], [90, 1975]]
Bharel
  • 23,672
  • 5
  • 40
  • 80
  • however, heapq will do insertions in the middle of existing lists, which is not efficient at all. Even though one can get away witha. sorted merged list, I'd say depending on the input data, if one tries to add item by item, it could be orders of magnitude slower than merging both lists and sorting then at once. – jsbueno Jan 19 '22 at 19:46
  • @Chris_Rands hehe already did, thanks :-) – Bharel Jan 19 '22 at 19:46
  • Oh, heapq.merge will do that in a single step - sorry - my comment above would apply if trying to add item by item from a second list to a first, ordered list. – jsbueno Jan 19 '22 at 19:48
  • @jsbueno heapq deals with it as efficiently as it can get. It simply returns the smallest of all iterators and advances that iterator. – Bharel Jan 19 '22 at 19:48
0

from where it has been already answered I've tried and worked:

list1 = [[40, 1980], [50, 1970], [70, 1980], [90, 1975]]
list2 = [[60, 1980],[65,1985]]

list3=sorted(list1 + list2)

print(list3)
DanielGzgzz
  • 118
  • 1
  • 6