I need a collection of elements the only purpose of which is to store elements for futher enumeration, so the elements don't have to be ordered/indexes in any way.
But a simple list is not enough, because I may need to remove any element from the collection, and I need to do it in constant time.
deque
or other implementation of linked list as data structures would be sufficient for this purpose, because deletion of an element in linked list is a constant time operation.
The problem is that python does not provide a way to track a specific node of a linked list (at least I don't know how), so if I need to remove previously added element from a list, first I need to find it, which is linear time operation.
In C++, there is a std::list
with its std::list::iterator
that does exactly what I need. The iterator holds the pointer to the node in the linked list. When I add an element to a linked list, I can store the iterator of the just added element and further use it to remove the element from the list in constant time, since I already have a pointer to the linked list node with the pointer to previous and next elements. It also doesn't matter whether and how many elements were added or deleted before or after the element.
So how can it be done in python? Is there some alternative to the C++'s std::list::iterator
?
I also want to notice that I can implement a linked list in python myself, but I'm afraid it would be slower than the implementation written in C. And I also don't want to reinvent the wheel.