3

Trying to display data in a structure that is a dictionary contained in a defaultdict

dictionary=defaultdic(dict)

Example

defaultdict = {key1} : 
                  {subkey1}:
                      (val1,
                       val2,
                       val3)
                  {subkey2}:
                      (val4,
                       val5,
                       val6)
              {key2} : 
                  {subkey3}:
                      (val7, 
                       val8, 
                       val9),
                  {subkey4}:
                      (val10,
                       val11,
                       val12)

I tried to do

for key in dictionary.iterkeys():
    print key # This will return me the key
    for items in dictionary[key]:
        print items # This will return me the subkey
        for values in dictionary[key][items]:
            print values #this return the values for each subkey)

The problem is that I just get printed out a flat list of items; which is almost impossible to follow when you have too many items and keys.

How do you properly print such complex structures, to present them in a way that does not make you rip your eyes out? I tried with pprint and json.dumps but neither was making the situation better. Ideally I would like to have it printed as in my example, but I can't see a simple way to do so, without going trough complex string manipulation to format the print output.

  • Convert them to json and use a pretty printing library for json? Or, within the nested loops append a tab character to the print. print "\t" + values so the values within the subkey will be offset – JustinDanielson Mar 12 '15 at 00:39
  • there are some good answers here you should look at - http://stackoverflow.com/questions/3229419/pretty-printing-nested-dictionaries-in-python – Calum Mar 12 '15 at 00:40
  • Thanks, but if I convert in JSON, then I need to parse the JSON to read again the data; I am not using JSON anywhere, that's why I was just trying to print out the overall structure, in case I have to dump it on screen for debugging. I didn't think about adding a tab when doing the print; thanks! –  Mar 12 '15 at 00:44
  • I assumed it was just for debugging, that PrettyPrint module for dictionaries seems to be the answer, it just skips the json part. I imagine the output is similar to JSON but without the curly braces. – JustinDanielson Mar 12 '15 at 00:49

2 Answers2

6

Python has the PrettyPrint module just for this purpose. Note that defaultdicts won't print nicely, but if you convert back to a regular dict first it'll do fine.

from pprint import pprint
pprint(dict(dictionary))
tzaman
  • 46,925
  • 11
  • 90
  • 115
  • Thanks a lot; I was not converting the defaultdict, that's why the print was horrible. –  Mar 12 '15 at 00:52
0

Use indentation to print them out so your eye can visually see the structure.

for key in dictionary.iterkeys():
    print key # This will return me the key
    for items in dictionary[key]:
        print("    %s" % items) # This will return me the subkey
        for values in dictionary[key][items]:
            print("        %s" % values) #this return the values for each subkey)

You can also substitute the space characters for \t to use a tab character; both will work fine. You may also have to use repr(values) or str(values) to explicitly get a string representation of them, if Python complains about the objects not being able to be formatted as a String.

Aaron D
  • 7,540
  • 3
  • 44
  • 48