Format list of list sub elements keeping structure.

Vlastimil Brom vlastimil.brom at
Tue Jul 24 04:04:01 EDT 2018

2018-07-24 3:52 GMT+02:00, Sayth Renshaw <flebber.crue at>:
> I have data which is a list of lists of all the full paths in a json
> document.
> How can I change the format to be usable when selecting elements?
> data = [['glossary'],
>  ['glossary', 'title'],
>  ['glossary', 'GlossDiv'],
>  ['glossary', 'GlossDiv', 'title'],
>  ['glossary', 'GlossDiv', 'GlossList'],
>  ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry'],
>  ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'ID'],
>  ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'SortAs'],
>  ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'GlossTerm'],
>  ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'Acronym'],
>  ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'Abbrev'],
>  ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'GlossDef'],
>  ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'GlossDef', 'para'],
>  ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'GlossDef',
> 'GlossSeeAlso'],
>  ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'GlossDef',
> 'GlossSeeAlso', 0],
>  ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'GlossDef',
> 'GlossSeeAlso', 1],
>  ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'GlossSee']]
> I am trying to change it to be.
> [['glossary'],
>  ['glossary']['title'],
>  ['glossary']['GlossDiv'],
> ....]
> Cheers
> Sayth
> --

You may try to experiment with pprint

I don't use it very often, but it seems to suit the needs like this.
If you increase the default width=80 (intended for reading and source
code) to a larger value, the formatting might come close to your
specification. For the sample data, the width can be set to a slightly
smaller value, than the length of the whole string representation of
the nested list, the formatting then follows the next "level" of the
nesting, these are formated on separate lines.

>>> import pprint
>>> len(str(data))
>>> pprint.pprint(data, width=956, compact=False)
 ['glossary', 'title'],
 ['glossary', 'GlossDiv'],
 ['glossary', 'GlossDiv', 'title'],
 ['glossary', 'GlossDiv', 'GlossList'],
 ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry'],
 ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'ID'],
 ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'SortAs'],
 ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'GlossTerm'],
 ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'Acronym'],
 ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'Abbrev'],
 ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'GlossDef'],
 ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'GlossDef', 'para'],
 ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'GlossDef',
 ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'GlossDef',
'GlossSeeAlso', 0],
 ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'GlossDef',
'GlossSeeAlso', 1],
 ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'GlossSee']]


More information about the Python-list mailing list