user2761895
user2761895

Reputation: 1541

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 ?

Upvotes: 0

Views: 269

Answers (1)

Scott Hunter
Scott Hunter

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

Related Questions