Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python nested dictionaries into reStructuredText bullet list

I have this dictionary:

d = {'B': {'ticket': ['two', 'three'], 'note': ['nothing to report']}, 'A': {'ticket': ['one'], 'note': ['my note']}, 'C': {'ticket': ['four'], 'note': ['none']}}

and I'm trying to convert it into a .rst document as a bullets list, like:

* A

  * ticket:

    * one

  * note

    * my note

* B

  * ticket:

    * two
    * three

  * note:

    * nothing to report

* C

  * ticket:

    * four

  * note:

    * none

I read this approach but I cannot translate it into a bullet list

Thanks to all

like image 216
matteo Avatar asked Apr 21 '26 13:04

matteo


1 Answers

For something like your specific example, what about this:

>>> for key, value in d.items():
...    print('* {}'.format(key))
...    for k, v in value.items():
...       print(' * {}:'.format(k))
...       for i in v:
...         print('  * {}'.format(i))
...
* B
 * note:
  * nothing to report
 * ticket:
  * two
  * three
* A
 * note:
  * my note
 * ticket:
  * one
* C
 * note:
  * none
 * ticket:
  * four
like image 109
Burhan Khalid Avatar answered Apr 23 '26 01:04

Burhan Khalid