How to easily display top level of data structure in python
Question: I am working with a (modestly) large complex structured data object in python.
It is something I've imported from json, so it's a hierarchical mixture of
dicts and lists. The data looks lovely in an online json-hierarchical browser.
However I have trouble navigating it in Python.

If I type

    
    
    pprint(data)
    

It gives me 30 pages of output in the console. What if I just want to list,
for example, the top two levels of the tree? So for example, if I have a list
of dicts (e.g. each of which has several keys containing several lists of keys
of lists), and at the lowest level there are numbers and strings.

How can I show (in text form) just the higher-level part?

In the mean time I have resorted to an IDE which has a tree view. But surely
it's possible in the console? This must be a perennial problem - people need
to do this all the time?


Answer: Yep, pretty print does that; from [the
documentation](https://docs.python.org/2/library/pprint.html), use the depth=n
keyword parameter:

    
    
    >>> tup = ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead',
    ... ('parrot', ('fresh fruit',))))))))
    >>> pp = pprint.PrettyPrinter(depth=6)
    >>> pp.pprint(tup)
    ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead', (...)))))))
    

You can pass that parameter directly to pprint.pprint:

    
    
    >>> pprint.pprint(tup, depth=6)
    ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead', (...)))))))
    

