I have a sorted list of tuples of the form
x =
[(0,1), (0,2), (0,3), ...
(1,1), (1,3), (1,4), ...
...
(n,0), (n,4), ...
]
I want to slice the list such that all numbers of (x,y) where x is a certain value in the new list and the order is kept. Now, this would obviously work:
y = [(a,b) for (a,b) in x if a == n]
But it is really slow. It would be faster to find the first and last index that satisfies this condition with binary search. index
gives you the first index for a value, and index
of the reversed list would give the last index. How would apply it though without doing [a for (a,b) in x]
and copying the whole list, in a pythonic way?