Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyCharm: format a dictionary in a tidy manner?

Tags:

pycharm

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
}
like image 974
Cybran Avatar asked Mar 13 '14 11:03

Cybran


People also ask

How do you format a Dictionary in Python?

Use format() function to format dictionary print in Python. Its in-built String function is used for the purpose of formatting strings according to the position. Python Dictionary can also be passed to format() function as a value to be formatted.

How do I get the Beautify Code in PyCharm?

The dialog appears when you press Ctrl+Alt+Shift+L in the editor of the current file. If you choose Code | Reformat Code from the main menu or press Ctrl+Alt+L , PyCharm tries to reformat the source code of the specified scope automatically.


1 Answers

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

like image 144
Yurii Avatar answered Sep 18 '22 19:09

Yurii