Suppose I have profiled my program, and most of the runtime is spent on the remove method from the list objects. The program manages the collection of collections, and collections do not need to be ordered. What would be the easiest way to implement these collections in python (preferably using standard python collections), so collection.remove (item) is inexpensive when the collection is an external collection and the item is an internal collection, and when the collection is an internal collection and an element is just an immutable object.
The problem with using sets here is that sets cannot contain mutable collections, so there should be freezets for internal sets, but then deleting items is no longer so cheap.
The best solution I came up with was suggested by someone as an answer here, which apparently was deleted shortly after. They suggested using a dict. This will work, but you will have to generate an arbitrary identifier for each element, so this is a bit inconvenient. Another alternative is to use a linked list, but that would also be inconvenient, since linked lists are not part of the standard library.
source share