'PyCharm: format a dictionary in a tidy manner?

say i have a dictionary in a file in the following format:

{'a': 1, 'b': 2, 'c': 3}

How do I format it 'tidily' in PyCharm? e.g.

{ 
    'a': 1, 
    'b': 2, 
    'c': 3
}


Solution 1:[1]

Go to

Editor -> Code style -> Python -> Wrapping and Braces -> Dictionary literals.

Set Wrap always instead of Wrap if long.

Then if you reformat the file or some selection, map literals will be formatted properly (like asked in the question).

enter image description here

Solution 2:[2]

You have to select the dict structure inside PyCharm, then [Code] > [Reformat Code...], then make sure the option "Selected text" is selected and hit Run.

Solution 3:[3]

If the need is to print a dict, then I recommend pretty printing like this:

from pprint import pprint

pprint(my_dict)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Manrique
Solution 2 Andrea M.
Solution 3 Hakun