Reputation: 1541
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 ?
Upvotes: 0
Views: 269
Reputation: 49803
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.)
Upvotes: 1