3

I often use nested dictionaries in Python 2.7 with 3 or more tiers and use a nested for loop structure, as shown below, to access each element. does anyone know of a simpler, neater or faster method to do so?

for foo in mydict:
    for bar in mydict[foo]:
        for etc in mydict[foo][bar]:
            mydict[foo][bar][etc] = "value"
Cœur
  • 37,241
  • 25
  • 195
  • 267
Tom Smith
  • 371
  • 2
  • 3
  • 14
  • 1
    You are not only accessing, but also assigning. So, you want to assign the same value to all the keys of all the nested dictionaries? – thefourtheye Apr 01 '14 at 13:30
  • i understand this is a general comment, but i use this method to access, assign, update variables etc. No I dont want to assign the same variable to each key, that could be done easily with many other methods, this is simply about a simple way to iterate over each element, if there is any simpler method than I already use – Tom Smith Apr 01 '14 at 13:34
  • This seems to solve your problem http://stackoverflow.com/questions/3797957/python-easily-access-deeply-nested-dict-get-and-set – Conans Apr 01 '14 at 13:39
  • Okay cheers, that looks detail heavy, I will have a good read and see what my simple mind can make out of it. – Tom Smith Apr 01 '14 at 13:42
  • How does `mydict` look like? Could you post an example? – skamsie Apr 01 '14 at 14:16
  • As previously mention, mydictdoes not look like anything, it varies between 2 to 5 tiers each usually, but not always with different values, be them either lists, floats or strings. This was just a general inquiry as to methods of iterating over a dictionary, especially in terms of simplicity/readability. – Tom Smith Apr 01 '14 at 16:58

2 Answers2

3

You're using keys to access values. How about using dict.itervalues() instead?

for foo in mydict.itervalues():
    for bar in foo.itervalues():
        for etc in bar: # Iterating a dictionary yields keys
            bar[etc] = "value"
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • that sounds interesting, I can try to benchmark it and see if it runs any quicker. It simplifies each line a little by removing the successive [], I am wondering if there is anyway to reduce the lines of code though. cheers. – Tom Smith Apr 01 '14 at 13:37
1

Let's say you have the following dictionary:

mydict = {'a':{'aa':{'aaa':1}}, 'b':{'bb':{'bbb':2}}, 'c':{'cc':{'ccc':3}}}

The code you posted in your example will produce the following result:

{'a': {'aa': {'aaa': 'value'}}, 'c': {'cc': {'ccc': 'value'}},
 'b': {'bb': {'bbb': 'value'}}}

Since your question also mentions a one liner you could use a nested dictionary comprehension but it is not necessarily faster nor neater:

mydict = {key:{inner_key:{core_key:'value'
          for core_key in inner_dict.iterkeys()}
          for inner_key, inner_dict in value.iteritems()}
          for key, value in mydict.iteritems()}

This will produce the same result as the code you posted.It is technically a one-liner even though for the sake of readability it should not be written in one line.

skamsie
  • 2,614
  • 5
  • 36
  • 48
  • ahh! the incomprehensible comprehension method, should really start using them more probably, they just never look as intuitive as a normal for loop, but that could be a good one to play around with. I often have multiple iterations in a script and the ability to put it all on one line can make the script as a whole feel more readable, even if the snippet of code itself is not so nice to read. Thanks – Tom Smith Apr 01 '14 at 17:02