[Tutor] assigning values to dictionary
Steve Willoughby
steve at alchemy.com
Fri Oct 3 20:07:06 CEST 2008
On Fri, Oct 03, 2008 at 11:02:34AM -0700, jeremiah wrote:
> i think i figured it out. The result is that I have a dictionary within
> a dictionary..i think.
>
> for example:
>
> {'mcdonalds': {'hamburger': 'big mac','drink':'coke'}}
>
> How would I go about itterating through this?
Depends on how you want to iterate through it (what
in the end you're trying to accomplish). One example:
for major_key in my_dict:
print "The stuff stored under", major_key, "is:"
for minor_key in my_dict[major_key]:
print "(%s) %s=%s" % (major_key, minor_key, my_dict[major_key][minor_key])
Another possibility might be:
for major_key, subdict in my_dict.iteritems():
print "The stuff stored under", major_key, "is:"
for minor_key, value in subdict.iteritems():
print "(%s) %s=%s" % (major_key, minor_key, value)
>
> Thanks,
> JJ
>
>
> On Fri, 2008-10-03 at 10:39 -0700, Steve Willoughby wrote:
> > On Fri, Oct 03, 2008 at 10:29:13AM -0700, jeremiah wrote:
> > > I have a list of dictionary values that i am looping through that
> > upon
> > > each iteration I would like to assign these values to a new
> > dictionary
> > > name.
> > >
> > > For example...
> > >
> > > item=0
> > > for line in some_dict:
> > > ## some how assign dict values to new name
> > > "new_dict_name_"+item = line
> >
> > You could use eval, I suppose, but are you sure that
> > making up new dictionary names on the fly like that is
> > the solution that best fits the problem as opposed to
> > having, say, a set of objects or an overall dictionary
> > of dictionaries you add to? What's the bigger context
> > this fits into? Although you *can* do this sort of thing,
> > it quite often ends up not being the most elegant thing
> > to do.
> >
> > --
> > Steve Willoughby | Using billion-dollar satellites
> > steve at alchemy.com | to hunt for Tupperware.
> >
> >
> >
>
>
> Disclaimer: The information contained in this transmission, including any
> attachments, may contain confidential information of Panasonic Avionics
> Corporation. This transmission is intended only for the use of the
> addressee(s) listed above. Unauthorized review, dissemination or other use
> of the information contained in this transmission is strictly prohibited.
> If you have received this transmission in error or have reason to believe
> you are not authorized to receive it, please notify the sender by return
> email and promptly delete the transmission.
>
>
--
Steve Willoughby | Using billion-dollar satellites
steve at alchemy.com | to hunt for Tupperware.
More information about the Tutor
mailing list