I am writing a function that takes a large list of strings and classifies them.
Each string classified is deleted from the list one after another.
Sometimes the function needs to delete a string in an arbitrary index and sometimes it needs to delete a string from the leftmost position.
The ratio between these two cases is about 2/3 of the times it deletes from the leftmost position and 1/3 of the time from an arbitrary place in the middle.
I know using list as the data structure to hold the strings is O(n) deleting the leftmost cell and O(n-i) deleting the i'th cell, since it moves all the cells to the right one step left for each deletion.
I then tried using collections.deque
instead of list, but it actually took more time (about 4/3 more time) than the original list
data structure, possibly because of the arbitrary place deletions.
What data structure will perform best under these assumptions? Is there any better than just using a list
?
The deletions with the list
data structure from leftmost cell with is using list.pop(0)
and removing from arbitrary index is by value using list.remove(value)
.