Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 276
Wizzard Avatar asked Oct 21 '10 08:10

Wizzard


People also ask

Which is better list or dictionary in Python?

Therefore, the dictionary is faster than a list in Python. It is more efficient to use dictionaries for the lookup of elements as it is faster than a list and takes less time to traverse. Moreover, lists keep the order of the elements while dictionary does not.

How do you format a dictionary in Python?

Use format() function to format dictionary print in Python. Its in-built String function is used for the purpose of formatting strings according to the position. Python Dictionary can also be passed to format() function as a value to be formatted.

Why is dict faster than list?

The reason is because a dictionary is a lookup, while a list is an iteration. Dictionary uses a hash lookup, while your list requires walking through the list until it finds the result from beginning to the result each time.

Does order matter in dictionary Python?

Since dictionaries in Python 3.5 don't remember the order of their items, you don't know the order in the resulting ordered dictionary until the object is created. From this point on, the order is maintained. Since Python 3.6, functions retain the order of keyword arguments passed in a call.


1 Answers

My preferred way is:

something = {'foo': 'bar',              'foo2': 'bar2',              'foo3': 'bar3',              ...              'fooN': 'barN'} 
like image 122
aaronasterling Avatar answered Sep 29 '22 01:09

aaronasterling