Python "best formatting practice" for lists, dictionary, etc

Viewed 35505

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?

11 Answers

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.

Related