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

w3schools python
python download
python syntax
introduction to python
what is python used for
python online
python wiki
python snake

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?


My preferred way is:

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

Python (programming language), Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java  Learn Python Like a Pro. From The Basics All The Way to Creating your own Apps and Games! Join Over 50 Million Students Already Learning Online With Udemy


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

Python Tutorial, Core Devs Say A Fond But Firm Farewell To Python 2 Master Python Code, Functions & Objects, CRUD Application & More - Start Today!


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.

Python Language Summit 2020, The class includes written materials, lecture videos, and lots of code exercises to practice Python coding. These materials are used within Google  >>> Python Software Foundation. The mission of the Python Software Foundation is to promote, protect, and advance the Python programming language, and to support and facilitate the growth of a diverse and international community of Python programmers. Learn more. Become a Member Donate to the PSF


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?

Snakes And Ladders: Game Boy Emulator In Python, Take-Away Skills: This course is a great introduction to both fundamental programming concepts and the Python programming language. By the end, you'll be  The Python web site provides a Python Package Index (also known as the Cheese Shop, a reference to the Monty Python script of that name). There is also a search page for a number of sources of Python-related information. Failing that, just Google for a phrase including the word ''python'' and you may well get


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.

Core Devs Say A Fond But Firm Farewell To Python 2, Python is a general-purpose, versatile and modern programming language. It's great as a first language because it is concise and easy to read. Use it for  Python is an interpreted, high-level, general-purpose programming language.Created by Guido van Rossum and first released in 1991, Python's design philosophy emphasizes code readability with its notable use of significant whitespace.


Google's Python Class | Python Education, learnpython.org is a free interactive Python tutorial for people who want to learn Python, fast. Python 3.8.0 - Oct. 14, 2019. Note that Python 3.8.0 cannot be used on Windows XP or earlier. Download Windows help file. Download Windows x86-64 embeddable zip file. Download Windows x86-64 executable installer. Download Windows x86-64 web-based installer. Download Windows x86 embeddable zip file. Download Windows x86 executable installer.


Learn Python 2, Learn Programming for Everybody (Getting Started with Python) from University of Michigan. This course aims to teach everyone the basics of programming  Python is a programming language. Python can be used on a server to create web applications. Start learning Python now »


Python Courses & Tutorials, Operators are the constructs which can manipulate the value of operands. Consider the expression 4 + 5 = 9. Here, 4 and 5 are called operands and + is called operator. Python language supports the following types of operators.