By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,701 Members | 1,966 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,701 IT Pros & Developers. It's quick & easy.

Newby Question: Python and A Nested Dictionary

P: n/a
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? The format that I am attempting is:

Select data ...
Get data ...
Place data into a nested dictionary
Rev[A] [b] = [500, 700, ...]
for value1 in a:
for value2 in Rev[A]
for value3 in Rev[A][b]
print something

kbass
Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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

Jul 18 '05 #2

P: n/a

"Greg Ewing (using news.cis.dfn.de)" <wm*******@sneakemail.com> wrote in
message news:c2*************@ID-169208.news.uni-berlin.de...
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


An extra would be:

Data Source: Select region, source from accounts;

I want to be able to store the data from the select statement into something
like:
revenue [source][region = return data from the select statement

To loop over the data, I would like to perform something like:
for src in revenue:
for reg in revenue[source]:
for value in revenue[source][region]
print something

Hope this makes sense. Thanks!

kbass
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.