So I have a list of 85 items. I would like to continually reduce this list in half (essentially a binary search on the items) — my question is then, what is the most efficient way to reduce the list? A list comprehension would continually create copies of the list which is not ideal. I would like in-place removal of ranges of my list until I am left with one element.
I’m not sure if this is relevant but I’m using collections.deque instead of a standard list. They probably work the same way more or less though so I doubt this matters.
For a mere 85 items, truthfully, almost any method you want to use would be more than fast enough. Don’t optimize prematurely.
That said, depending on what you’re actually doing, a
listmay be faster than adeque. Adequeis faster for adding and removing items at either end, but it doesn’t support slicing.With a list, if you want to copy or delete a contiguous range of items (say, the first 42) you can do this with a slice. Assuming half the list is eliminated at each pass, copying items to a new list would be slower on average than deleting items from the existing list (deleting requires moving the half of the list that’s not being deleted “leftward” in memory, which would be about the same time cost as copying the other half, but you won’t always need to do this; deleting the latter half of a list won’t need to move anything).
To do this with a
dequeefficiently, you would want topop()orpopleft()the items rather than slicing them (lots of attribute access and method calls, which are relatively expensive in Python), and you’d have to write the loop that controls the operation in Python, which will be slower than the native slice operation.Since you said it’s basically a binary search, it is probably actually fastest to simply find the item you want to keep without modifying the original container at all, and then return a new container holding that single item. A
listis going to be faster for this than adequesince you will be doing a lot of accessing items by index. To do this in adequewill require Python to follow the linked list from the beginning each time you access an item, while accessing an item by index is a simple, fast calculation for alist.