need dict to maintain order

Steve Holden sholden at holdenweb.com
Fri Jan 18 11:39:36 EST 2002


"rihad" <rihad at mail.ru> wrote ...
> Hi there, I'm trying to automate the process of creating so called
> `webpage navbars' by writing an offline python script, but dict
> doesn't seem to give its items in defined order:
>
> links = {'index.html': 'Home', 'file0.html': 'Some foo'}
>
> this results in
> <div class="navbar_horz">
> [<a href="file0.html">Some foo</a>
>  | <a href="index.html">Home</a>
> ]
> </div>
>
> which is backwards. And yes, I'm using
>
> link, caption = links.popitem()
>
> Which other type would you recommend?
>
The dictionary type is specifically not required to provide its elements in
any order!

Rather than using a dictionary and then (presumably) a while loop until the
dictionary is empty, consider setting up a list or tuple of tuples, such as

    links = (('index.html', 'Home'), ('file0.html', 'Some foo'))

The you can use

    for link, caption in links:
        # generate nav bar line from link and capion

regards
 Steve
--
Consulting, training, speaking: http://www.holdenweb.com/
Python Web Programming: http://pydish.holdenweb.com/pwp/








More information about the Python-list mailing list