Correct layout of dictionary literals

I've seen several different ways that people prefer to format dictionaries, but most people seem to follow 1 of 2 ways:

Option 1)

d = {
    'key1': 'value1',
    'key2': 'value2'
    }

      

Option 2)

d ={'key1': 'value1', 'key2': 'value2'}

      

In use both do the same thing, but are just another python, is the best way to format a dictionary, just wrong?

I'm curious to know which formatting method is most widely accepted and best used in my scripts.

Before I was informed that I hadn't done a lot of research, I did it and it caused even more confusion, different sites, different people, different tutorials, often using different methods, and I can't find anywhere that says, "Do this for example this is the correct syntax "

+3


source to share


4 answers


This is discussed in PEP 8, in the indentation section .

Continuation lines must align wrapped elements either vertically using the Python implicit line concatenated in parentheses, parentheses and curly braces, or using hanging indentation [5]

So dictionaries are covered there.

... brackets and curly braces, ...

And all it does is provide ways to do several kinds of multi-line statements;

# Aligned with opening delimiter.
foo = long_function_name(var_one, var_two,
                         var_three, var_four)

# More indentation included to distinguish this from the rest.
def long_function_name(
        var_one, var_two, var_three,
        var_four):
    print(var_one)

# Hanging indents should add a level.
foo = long_function_name(
    var_one, var_two,
    var_three, var_four)

      

So, in your examples;



d ={'key1': 'value1', 'key2': 'value2'}

      

doesn't actually adhere to the PEP8 space in expressions and statements because you missed the space; it should be (note the extra space. Yes, it's pedantic):

d = {'key1': 'value1', 'key2': 'value2'}

      

As for the multi-line version, all the examples from PEP8 leave the closing parenthesis on the last line (but it doesn't specifically mention where it should go). There are some alternatives as well, and you choose seemingly just a preference - "Silly Reconciliation" is the Hobgoblin of Little Minds (also PEP8). The main rule:

When using hanging indentation, consider the following considerations: applies; there should be no arguments on the first line and further indentation should be used to clearly distinguish itself as a continuation of the line.

However, only the hanging indent is applied here and is aligned with the opening separator version.

# Aligned with opening delimiter.
d = {'key1': 'value1',
     'key2': 'value2'}

longerVariableName = {'key1': 'value1',
                      'key2': 'value2'}

# Hanging indents should add a level.
d = {
    'key1': 'value1',
    'key2': 'value2'}

longerVariableName = {
    'key1': 'value1',
    'key2': 'value2'}

      

+5


source


They are both the right paths. But if you initialize a dictionary with 100 pairs in it. The first option makes it more readable.



The seccond option is good for small dictionaries with one or two pairs

+1


source


For the translator, they are equivalent. In any particular case, use whatever syntax you think is easier for the other person.

This is subjective, but I personally would only use the second syntax for the smallest dictionaries.

0


source


There are several aspects to PEP8.

  • Renaming a variable or function name should not interrupt formatting. The new name can be shorter or longer.

  • Adding / removing an element should not affect the lines immediately above or below. You want git diff

    - or another VCS - to only display the changes.

If you agree with these points, you probably want to write a dicts that doesn't fit in one short line like this:

my_first_dict_today = {
    key1: value1,
    key2: value2,
    key3: value3,
    }

# note the comma after value3

      

The closing parenthesis can be aligned with either the last line or the first line, PEP8 allows both.

0


source







All Articles