Iterating Over Dictionary From Arbitrary Location

Aahz aahz at pythoncraft.com
Sun Jun 7 18:42:11 CEST 2009


In article <mailman.1241.1244301490.8015.python-list at python.org>,
akindo  <akindo01 at hotmail.com> wrote:
>
>So, it seems I want the best of both worlds: specific indexing using  
>my own IDs/keys (not just by list element location), sorting and the  
>ability to start iterating from a specific location. I am trying to  
>prevent having to scan through a list from the beginning to find the  
>desired ID, and then return all elements after that. Is there another  
>data structure or design pattern which will do what I want? Thanks a  
>lot for any help! :)

One option is to maintain both a dict and a list; it doesn't cost much
memory because the individual elements don't get copied.  See various
"ordered dictionary" implementations for ideas on coding this.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"If you don't know what your program is supposed to do, you'd better not
start writing it."  --Dijkstra



More information about the Python-list mailing list