views:

165

answers:

9

Been looking over the python docs for code formatting best practice for large lists and dictionaries, eg

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',
             ...
             }

and then how to handle deep nesting of lists/dictionaries...

+1  A: 

I prefer the second or third one.

Reason:

  1. Each element is on it's own line
  2. reaching to end of line to add a new element is a pain in text editor
  3. Adding a new element is easy
  4. With the third option, Sometimes you can check no of elements by selecting those lines. Most editor will tell you the number of selected lines.
pyfunc
#4 works with the second option too.
aaronasterling
@aaronasterling: Yup, I should have written second and third option.
pyfunc
I don't agree with #2. Use a text editor that powers you and not restricts you.
Török Gábor
+1  A: 

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.

klausbyskov
+1  A: 

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?

eumiro
OP doesn't want to know how to print it but how to format it IN the source code.
aaronasterling
@aaronasterling - of course. This can show him, how to format a given structure in his source code.
eumiro
@eumiro: so how does pprint print out? second or third?
Ashish
@Ashish - first for short, second for longer structures
eumiro
+8  A: 

My preferred way is:

something = {'foo': 'bar',
             'foo2': 'bar2',
             'foo3': 'bar3',
             ...
             'fooN': 'barN'}
aaronasterling
+1 it conforms to PEP8.
Török Gábor
A: 

Definitly NOT option 1, one of the strenghts of python is its legibility. Option 1 serverly diminishes that legibility.

Out of 2 & 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 ...

ianaré
+1  A: 

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.

Ashish
This does not conform to PEP8 because there is whitespace before ':' (E203).
Török Gábor
@Török Gábor: copy paste error :P
Ashish
+3  A: 

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',
}
danlei
That's not my style :)
Török Gábor
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.
AndrewF
@AndrewF I must agree. That's one of the only things JSON makes me angry about, in refusing to handle a trailing comma in sequences.
AndrewBC
@Török Sorry, changed it.
danlei
It must have been C/Java programmers voting on that because they saw something familiar.
aaronasterling
A: 

I love the second way:

something = {'foo' : 'bar',
         'foo2' : 'bar2',
         'foo3' : 'bar3',
         ...
         'fooN': 'barN'}
kasx93
A: 

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.

jaydel