Python best formatting practice for lists, dictionary, etc

2024/11/19 1:38:24

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?

Answer

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.

https://en.xdnf.cn/q/26501.html

Related Q&A

TypeError: string indices must be integers, not str // working with dict [duplicate]

This question already has answers here:Why am I seeing "TypeError: string indices must be integers"?(10 answers)Closed 28 days ago.I am trying to define a procedure, involved(courses, person…

Pandas: create dataframe from list of namedtuple

Im new to pandas, therefore perhaps Im asking a very stupid question. Normally initialization of data frame in pandas would be column-wise, where I put in dict with key of column names and values of li…

Closest equivalent of a factor variable in Python Pandas

What is the closest equivalent to an R Factor variable in Python pandas?

Temporarily Disabling Django Caching

How do you disable Django caching on a per checkout basis?Back before Django 1.3, I could disable caching for my local development checkout by specifying CACHE_BACKEND = None, in a settings_local.py i…

How to get a complete exception stack trace in Python

The following snippet:import tracebackdef a():b()def b():try:c()except:traceback.print_exc()def c():assert Falsea()Produces this output:Traceback (most recent call last):File "test.py", line …

python - should I use static methods or top-level functions

I come from a Java background and Im new to python. I have a couple scripts that share some helper functions unique to the application related to reading and writing files. Some functions associated …

Draw graph in NetworkX

Im trying to draw any graph in NetworkX, but get nothing, not even errors:import networkx as nx import matplotlib.pyplot as plt g1=nx.petersen_graph() nx.draw(g1)

Django 1.7 migrations wont recreate a dropped table, why?

Using Django 1.7 migrations.I accidentally dropped a table in my database. I assumed that by running migration again this would recreate the table but no, Django states "No migrations to apply&quo…

Reset ipython kernel

I was wondering if there is a way to restart the ipython kernel without closing it, like the kernel restart function that exists in the notebook. I tried %reset but that doesnt seem to clear the import…

What is the best way to remove a dictionary item by value in python? [duplicate]

This question already has answers here:Removing entries from a dictionary based on values(4 answers)Closed 4 years ago.I wonder if there is simple way to remove one or more dictionary element(s) from a…