Question

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

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?

 45  42501  45
1 Jan 1970

Solution

 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.

2013-08-09

Solution

 42

My preferred way is:

something = {'foo': 'bar',
             'foo2': 'bar2',
             'foo3': 'bar3',
             ...
             'fooN': 'barN'}
2010-10-21