views:

307

answers:

5

What do you think is the convention that is mostly used when writing dictionary literals in the code?

I'll write one possible convention as an answer.

+11  A: 
my_dictionary = {
    1: 'something',
    2: 'some other thing',
}
hcs42
+1 for the trailing comma.
RichieHindle
At first, I did not appreciate the utility of the trailing comma in lists, dicts, etc. But it *really* simplifies subsequent editing of the list, whether you are reordering, copy/pasting, or adding new items.
Paul McGuire
Adding one line is a one-line diff with the comma in place.
kaizer.se
Any other format is blasphemy! Just kidding :) This is definitely my far and away favorite though.
TM
+12  A: 

I'd say there is almost no standard.

I've seen two ways of indenting:

Indent 1:
my_dictionary = {
    'uno': 'something',
    'number two': 'some other thing',
}

Indent 2:
my_dictionary = {'uno': 'something',
                 'number two': 'some other thing',
                 }

I've seen three placed to have the end bracket:

End 1:
my_dictionary = {'uno': 'something',
                 'number two': 'some other thing',
}

End 2:
my_dictionary = {'uno': 'something',
                 'number two': 'some other thing',
                 }

End 3:
my_dictionary = {'uno': 'something',
                 'number two': 'some other thing',}

And sometimes you justify the values:

my_dictionary = {'uno':        'something',
                 'number two': 'some other thing',
                 }

And sometimes even the colons:

my_dictionary = {'uno'        : 'something',
                 'number two' : 'some other thing',
                 }

Which looks weird.

And sometimes you have an end comma, and sometimes not:

my_dictionary = {'uno': 'something',
                 'number two': 'some other thing'}

And sometimes you stick it all on one row (if it fits).

my_dictionary = {'uno': 'something', 2: 'some other thing'}

Everyone seems to have their own combination of these styles. Personally I tend towards the style you use in your example, unless there is a reason not to. Common reasons not to is when you have a dictionary as a part of a statement. Like so:

amethodthattakesadict({'hey': 'this',
                       'looks': 'a',
                       'bit': 'shitty',
                      })

I'd recommend that you adapt yourself to the style of the guy who wrote the code you are editing. If it's your code: Do as you like. :-)

Lennart Regebro
+1 for nicely describing all the possibilities.
hcs42
I think the only part of those examples actually governed by PEP8 is the spacing around the colons. Zero before, one after.
Triptych
+5  A: 

About the end brace: I prefer it like this:

my_dictionary = {
   'a': 'first value',
   'b': 'second',
   }

and I'll tell you why: because Python code indentation has no close token, code is indented like that: the first line (if, while, def, etc) is outdented from the rest of the clause, with all the other lines indented the same amount. The last line of the clause is indented along with everything else. The next line indented the same as the first line is the first line of the next clause, not the last line of this one.

So I like to indent data structures using a similar convention to that of code clauses, even though data structures have an explicit closing token, and so could have more flexibility.

Ned Batchelder
A: 

I do this, if the dictionary is too large to fit on a single line:

d = \
   {
   'a' : 'b',
   'c' : 'd'
   }
pzr
+2  A: 

Indentation style 1, ending style 3 (after Lennart's answer):

   my_dictionary = {1: 'thing_one',
                    2: 'thing_two',
                    ...
                    n: 'thing_n'}

This might be the most seamless indentation style for bracketed entities in Python code, because it closely resembles Python's whitespace formatting. Dropping to C-style indentation in Python code always seemed a little awkward to me, and I suspect that it is mostly used because programmers accustomed to C-like languages (because of their ubiquity) have maybe not been exposed to different indentation styles.

A drawback might be that insertion at the beginning or end is a little harder than in other styles. Considering a proper editor which supports writing Python code, it should not make that much of a difference.

Try this indentation style in context, and compare it to C-style indentation side by side, then decide which one is looking more pythonic and coherent.

Maybe this could be called lisp-style indentation, because it is the way in which lisp code has been indented since centuries decades, but it is, for example, also often used in Smalltalk code. One thing you will often read in discussions about the placement of parenthesis (in Lisp like languages) is: "Why give them extra lines? Are they that important?".

At the end of the day though, it is mostly a taste thing.