an indexed list - how to do it in python

Alex cut_me_out at hotmail.com
Sat Aug 12 13:33:40 EDT 2000


> I want to create, in Python, an object that mixes the behavior of a
> sequence and a mapping. The primary behavior should be that of a
> mutable sequence, or list, but the objects in the list should also be
> accessible by a key value.
> 
> Given an object and a list to which it belongs, how can I find its
> position in a list in O(1) operations.

I can't think of a way to do this that doesn't involve periodically
updating an index into the list.  Why do you want to do it?

If you just want fast access to the objects using the mapping, and
preservation of an ordering on the objects, I would use a linked list
instead of a python list.  That way you can keep a mapping to a linked
list wrapper around the objects you want to store, and do fast
insertions and deletions without knowing the absolute position of the
relevant list elements.

Alex.

-- 
To succeed in the world it is not enough to be stupid; you must also be
well-mannered. -- Voltaire




More information about the Python-list mailing list