Newby Question: Python and A Nested Dictionary

Greg Ewing (using news.cis.dfn.de) wmwd2zz02 at sneakemail.com
Thu Mar 11 22:50:43 EST 2004


kbass wrote:
> I am new to Python and I am attempting to retrieve data from a database and
> I would like to place this data into a nested dictionary. After placing the
> data into a dictionary, I would like to loop through the data using for
> loops. How would I do this?

It's hard to say exactly how to go about building the dictionary
without knowing more about the format of the data and how you
want to structure it. Posting some sample data together with
the dict you want to build from it would help.

As for looping over the dictionaries, some general facts you
might find useful are:

    for key in dictionary:
       ...

will loop over all the keys in the dictionary.

    for value in dictionary.values():
       ...

will loop over the values (but you won't know what
their keys were).

    for key, value in dictionary.items():
       ...

will loop over all the key/value pairs in the dictionary.

-- 
Greg Ewing, Computer Science Dept,
University of Canterbury,	
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg




More information about the Python-list mailing list