'bisect.insort for list of lists in python 3.9.1

I'm using python 3.9.1 and trying to understand the bisect in 2d array.

I have a list of list like below and trying to insert a value to it using bisect.insort, but nothing works.

How to make it work?

l = [[[-1, 0], [0, 5], [3, 5]],
     [[-1, 0], [2, 6]],
     [[-1, 0], [1, 10]]]

l.sort(key=lambda x: x[1])

bisect.insort(l, 4)             # 1
bisect.insort(l, [4])           # 2
bisect.insort(l, [4,1])         # 3
print(f"l ={l}")

all three #1 - #3 are throwing TypeError.

How to insert a list to the list of lists by using insort in ?



Solution 1:[1]

You stopped one version too soon:

bisect.insort(l, [[4,1]])

Which may not produce what you want, but you never made clear exactly what that was. (And l is a list of lists of lists.)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Scott Hunter