Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Naming Conventions for Dictionaries/Maps/Hashes

While other questions have tackled the broader category of sequences and modules, I ask this very specific question:

"What naming convention do you use for dictionaries and why?"

Some naming convention samples I have been considering:

# 'value' is the data type stored in the map, while 'key' is the type of key value_for_key={key1:value1, key2,value2} value_key={key1:value1, key2,value2} v_value_k_key={key1:value1, key2,value2} 

Don't bother answering the 'why' with "because my work tells me to", not very helpful. The reason driving the choice is more important. Are there any other good considerations for a dictionary naming convention aside from readability?

EDIT:

Chosen answer: value_key_map

Reason for chosen answer: Allows a code reviewer to quickly and easily figure out the key and value for a map, and the fact that it is a map without looking anywhere else.

like image 436
pokstad Avatar asked Mar 09 '10 00:03

pokstad


People also ask

How do you name a dictionary in Python?

You declare a dictionary with a set of curly braces, {} . Inside the curly braces you have a key-value pair. Keys are separated from their associated values with colon, : .

Are maps and dictionaries the same in Python?

Dictionaries are often also called maps, hashmaps, lookup tables, or associative arrays. They allow the efficient lookup, insertion, and deletion of any object associated with a given key.

Can we name a dictionary in Python?

Objects don't have names in Python, a name is an identifier that can be assigned to an object, and multiple names could be assigned to the same one. However, an object-oriented way to do what you want would be to subclass the built-in dict dictionary class and add a name property to it.


1 Answers

key_to_value, for example surname_to_salary may be useful when there are closely interrelated maps in code: a to b, b to a, c to b etc.

like image 122
DSblizzard Avatar answered Oct 09 '22 12:10

DSblizzard