Python "best formatting practice" for lists, dictionary, etc
Asked Answered
S

11

45

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?

Sabbat answered 21/10, 2010 at 8:27 Comment(0)
P
42

My preferred way is:

something = {'foo': 'bar',
             'foo2': 'bar2',
             'foo3': 'bar3',
             ...
             'fooN': 'barN'}
Perjure answered 21/10, 2010 at 8:44 Comment(2)
Slightly less git-friendly in that changing key/values on first or last line pull in other syntax elements into changeset.Subside
what about nested dictionaries?Potentiometer
C
43

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.

Chiliarch answered 9/8, 2013 at 2:31 Comment(0)
P
42

My preferred way is:

something = {'foo': 'bar',
             'foo2': 'bar2',
             'foo3': 'bar3',
             ...
             'fooN': 'barN'}
Perjure answered 21/10, 2010 at 8:44 Comment(2)
Slightly less git-friendly in that changing key/values on first or last line pull in other syntax elements into changeset.Subside
what about nested dictionaries?Potentiometer
M
36

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',
}
Misogyny answered 21/10, 2010 at 10:22 Comment(3)
I particularly like that python allows you to follow the last item of a dictionary, list, or tuple with a comma. This makes it easier to re-order or to extend the sequence later.Indent
@Indent I must agree. That's one of the only things JSON makes me angry about, in refusing to handle a trailing comma in sequences.Fennessy
It must have been C/Java programmers voting on that because they saw something familiar.Perjure
M
7

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?

Mesquite answered 21/10, 2010 at 8:42 Comment(5)
OP doesn't want to know how to print it but how to format it IN the source code.Perjure
@Perjure - of course. This can show him, how to format a given structure in his source code.Mesquite
so how does pprint print out? second or third?Excoriate
@Ashish - first for short, second for longer structuresMesquite
Yes, as pprint is in the standard library, it provides a preferable style to follow according to the Zen of Python motto "There should be one-- and preferably only one --obvious way to do it."Hathaway
E
3

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.

Excoriate answered 21/10, 2010 at 8:51 Comment(1)
This does not conform to PEP8 because there is whitespace before ':' (E203).Brabazon
C
2

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 ...

Credential answered 21/10, 2010 at 8:46 Comment(0)
A
1

I prefer the second or third one.

Reason:

  1. Each element is on its own line
  2. Reaching to end of line to add a new element is a pain in a text editor
  3. Adding a new element is easy
  4. 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.
Appellation answered 21/10, 2010 at 8:30 Comment(2)
@aaronasterling: Yup, I should have written second and third option.Appellation
I don't agree with #2. Use a text editor that powers you and not restricts you.Brabazon
B
1

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.

Bold answered 21/10, 2010 at 8:31 Comment(0)
R
0

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.

Reinhardt answered 21/10, 2010 at 14:20 Comment(0)
P
-1

I love the second way:

something = {'foo' : 'bar',
         'foo2' : 'bar2',
         'foo3' : 'bar3',
         ...
         'fooN': 'barN'}
Psalter answered 21/10, 2010 at 11:45 Comment(0)
B
-1

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.

Bookstand answered 13/7, 2018 at 9:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.