I have a list like this for example:
L = [(0, "test", "value"), (0, "test2", "value2")]
and I need to = bisect_left the first item of each tuple to find an index in the list. However, I can’t think of a way of doing this without creating a list of all of these first items before hand:
exampleList = [L[i][0] for i in range(len(L))]
any ideas on another way to do this as it works but when my list gets to 5000 items long it is very slow and I need a faster way of doing it?
Advertisement
Answer
import bisect L = [(0, "test", "value"), (1, "test2", "value2"),(3,"test3","value3")] print (bisect.bisect_left(L,(2,)))
python deals with tuple comparisons great… just pass it (2,"asd","dsa")
or even just (2,)
(or whatever your index location item you are trying to find)
or you could pass it (0,"asd","value")
and it would know to put it to the left of the other (0,"test","value")
item tuple … or (0,"zzzz","value")
and it would know to put it to the right of that item