Python "best formatting practice" for lists, dictionary, etc
Question:
I have been looking over the Python documentation for code formatting best practice for large lists and dictionaries, for example,
something = {'foo' : 'bar', 'foo2' : 'bar2', 'foo3' : 'bar3'..... 200 chars wide, etc..}
or
something = {'foo' : 'bar',
'foo2' : 'bar2',
'foo3' : 'bar3',
...
}
or
something = {
'foo' : 'bar',
'foo2' : 'bar2',
'foo3' : 'bar3',
...
}
How do I handle deep nesting of lists/dictionaries?
Answers:
I prefer the second or third one.
Reason:
- Each element is on its own line
- Reaching to end of line to add a new element is a pain in a text editor
- Adding a new element is easy
- With the third option, sometimes you can check the number of elements by selecting those lines. Most editors will tell you the number of selected lines.
Well, the first one is a no-go, since your lines should only 79 characters wide. With regards to the other two options, I suppose it’s a matter of taste, but I personally prefer the second option.
Define your dictionary in any way you want and then try this:
from pprint import pprint
pprint(yourDict)
# for a short dictionary it returns:
{'foo': 'bar', 'foo2': 'bar2', 'foo3': 'bar3'}
# for a longer/nested:
{'a00': {'b00': 0,
'b01': 1,
'b02': 2,
'b03': 3,
'b04': 4,
'b05': 5,
'b06': 6,
'b07': 7,
'b08': 8,
'b09': 9},
'a01': 1,
'a02': 2,
'a03': 3,
'a04': 4,
'a05': 5,
'a06': 6,
'a07': 7,
'a08': 8,
'a09': 9,
'a10': 10}
Do you like the output?
My preferred way is:
something = {'foo': 'bar',
'foo2': 'bar2',
'foo3': 'bar3',
...
'fooN': 'barN'}
Definitely NOT option 1, one of the strenghts of Python is its legibility. Option 1 severely diminishes that legibility.
Out of 2 and 3, I’ll echo the same reasons pyfunc stated for both.
However, in my own code I prefer option 3 simply because the first element sometimes gets ‘lost’ by being at the end of the declare line, and upon quick glancing at code sometimes I just won’t see it immediately. I know it’s a little silly, but the mind works in mysterious ways …
If you go by ganeti (which respects PEP 8) you should choose the third option.
something = {
'foo1': 'bar1',
'foo2': 'bar2',
'foo3': 'bar3',
...
}
I like this esp. because you can select only the elements you want. And I feel removing or adding elements to either ends is faster this way.
Note: As pointed out in the comment there should be no whitespace before ‘:’ (E203) as per PEP.
aaronasterling’s indentation style is what I prefer. This, and several other styles are explained in another SO Question. Especially Lennart Regebro’s answer gave a nice overview.
But this style was the one most voted for:
my_dictionary = {
1: 'something',
2: 'some other thing',
}
I love the second way:
something = {'foo' : 'bar',
'foo2' : 'bar2',
'foo3' : 'bar3',
...
'fooN': 'barN'}
Previous to reading this post I would have opted for the third option you give. But now I might go for the one that is NOT Török Gábor’s style:
my_dictionary = {
1: ‘something’,
2: ‘some other thing’,
}
But honestly anything aside from your first option is probably fine.
According to the PEP8 style guide there are two ways to format a dictionary:
mydict = {
'key': 'value',
'key': 'value',
...
}
OR
mydict = {
'key': 'value',
'key': 'value',
...
}
If you want to conform to PEP8 I would say anything else is technically wrong.
I want to mention the following option, which is not specifically mentioned in the PEP8, but is noted in the dictionary documentation: “When the keys are simple strings, it is sometimes easier to specify pairs using keyword arguments:”
my_dict = dict(
foo = 1,
bar = 2,
baz = 3,
...
)
It doesn’t settle the indentation question, however.
I have been looking over the Python documentation for code formatting best practice for large lists and dictionaries, for example,
something = {'foo' : 'bar', 'foo2' : 'bar2', 'foo3' : 'bar3'..... 200 chars wide, etc..}
or
something = {'foo' : 'bar',
'foo2' : 'bar2',
'foo3' : 'bar3',
...
}
or
something = {
'foo' : 'bar',
'foo2' : 'bar2',
'foo3' : 'bar3',
...
}
How do I handle deep nesting of lists/dictionaries?
I prefer the second or third one.
Reason:
- Each element is on its own line
- Reaching to end of line to add a new element is a pain in a text editor
- Adding a new element is easy
- With the third option, sometimes you can check the number of elements by selecting those lines. Most editors will tell you the number of selected lines.
Well, the first one is a no-go, since your lines should only 79 characters wide. With regards to the other two options, I suppose it’s a matter of taste, but I personally prefer the second option.
Define your dictionary in any way you want and then try this:
from pprint import pprint
pprint(yourDict)
# for a short dictionary it returns:
{'foo': 'bar', 'foo2': 'bar2', 'foo3': 'bar3'}
# for a longer/nested:
{'a00': {'b00': 0,
'b01': 1,
'b02': 2,
'b03': 3,
'b04': 4,
'b05': 5,
'b06': 6,
'b07': 7,
'b08': 8,
'b09': 9},
'a01': 1,
'a02': 2,
'a03': 3,
'a04': 4,
'a05': 5,
'a06': 6,
'a07': 7,
'a08': 8,
'a09': 9,
'a10': 10}
Do you like the output?
My preferred way is:
something = {'foo': 'bar',
'foo2': 'bar2',
'foo3': 'bar3',
...
'fooN': 'barN'}
Definitely NOT option 1, one of the strenghts of Python is its legibility. Option 1 severely diminishes that legibility.
Out of 2 and 3, I’ll echo the same reasons pyfunc stated for both.
However, in my own code I prefer option 3 simply because the first element sometimes gets ‘lost’ by being at the end of the declare line, and upon quick glancing at code sometimes I just won’t see it immediately. I know it’s a little silly, but the mind works in mysterious ways …
If you go by ganeti (which respects PEP 8) you should choose the third option.
something = {
'foo1': 'bar1',
'foo2': 'bar2',
'foo3': 'bar3',
...
}
I like this esp. because you can select only the elements you want. And I feel removing or adding elements to either ends is faster this way.
Note: As pointed out in the comment there should be no whitespace before ‘:’ (E203) as per PEP.
aaronasterling’s indentation style is what I prefer. This, and several other styles are explained in another SO Question. Especially Lennart Regebro’s answer gave a nice overview.
But this style was the one most voted for:
my_dictionary = {
1: 'something',
2: 'some other thing',
}
I love the second way:
something = {'foo' : 'bar',
'foo2' : 'bar2',
'foo3' : 'bar3',
...
'fooN': 'barN'}
Previous to reading this post I would have opted for the third option you give. But now I might go for the one that is NOT Török Gábor’s style:
my_dictionary = {
1: ‘something’,
2: ‘some other thing’,
}
But honestly anything aside from your first option is probably fine.
According to the PEP8 style guide there are two ways to format a dictionary:
mydict = {
'key': 'value',
'key': 'value',
...
}
OR
mydict = {
'key': 'value',
'key': 'value',
...
}
If you want to conform to PEP8 I would say anything else is technically wrong.
I want to mention the following option, which is not specifically mentioned in the PEP8, but is noted in the dictionary documentation: “When the keys are simple strings, it is sometimes easier to specify pairs using keyword arguments:”
my_dict = dict(
foo = 1,
bar = 2,
baz = 3,
...
)
It doesn’t settle the indentation question, however.