In Python, I want to write a multi-line dict in my code. There are a couple of ways one could format it. Here are a few that I could think of:
-
mydict = { "key1": 1, "key2": 2, "key3": 3, } -
mydict = { "key1": 1, "key2": 2, "key3": 3, } -
mydict = { "key1": 1, "key2": 2, "key3": 3, }
I know that any of the above is syntactically correct, but I assume that there is one preferred indentation and line-break style for Python dicts. What is it?
Note: This is not an issue of syntax. All of the above are (as far as I know) valid Python statements and are equivalent to each other.
I use #3. Same for long lists, tuples, etc. It doesn’t require adding any extra spaces beyond the indentations. As always, be consistent.
Similarly, here’s my preferred way of including large strings without introducing any whitespace (like you’d get if you used triple-quoted multi-line strings):