Python "best Formatting Practice" For Lists, Dictionary, Etc
Solution 1:
My preferred way is:
something = {'foo': 'bar',
'foo2': 'bar2',
'foo3': 'bar3',
...
'fooN': 'barN'}
Solution 2:
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',
}
Solution 3:
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.
Solution 4:
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?
Solution 5:
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.
Post a Comment for "Python "best Formatting Practice" For Lists, Dictionary, Etc"