Search code examples
pythondictionaryrestructuredtext

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


Solution

  • 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