Python Naming Conventions for Dictionaries/Maps/Hashes

PythonDictionaryNaming ConventionsMapping

Python Problem Overview


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.

Python Solutions


Solution 1 - Python

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.

Solution 2 - Python

I never seem to name them anything like what you proposed (i.e. keeping one way). It just seems to be much more clear when I can find a "proper name" for the hash. It might be "person_details" or "file_sizes" or "album_tracks" etc. (although the last 2 seem to have key_value names, first one a bit less). In rare cases, it will be key_value_map, or value_key_map if it's important that it's a map.

I would never assume any naming scheme for that. Sometimes the values are what you're after, sometimes the keys. My preference is "a natural name".

Solution 3 - Python

values_by_key

  1. it is not so confusing as value_key_map: you can't confuse what is value name and what is key name
  2. it doesn't name the type directly -- python style naming

Solution 4 - Python

I think it makes sense to name the dict after the values in the dict, and drop any mention of the key. After all, you are going to be using the dict in situations like values[key] which makes it perfectly clear what the keys are, assuming you named key well.

Solution 5 - Python

I will contrast with many answers so far (including the chosen answer) and say:

I avoid adding type references like dict or map to the variable name. It feels too much like Hungarian notation to me, which feels very anti-pythonic.

To answer the question of what I DO use:

I use names of the form values, values_by_key or values_for_key, whichever reads most naturally.

Solution 6 - Python

I usually use <something>map since it's usually a map such as strings to functions, or numbers to classes, or whatnot. Unnamed dicts usually end up in a larger structure, so I don't worry about them.

Solution 7 - Python

In our projects, we adopted following convention:

  • key_to_value_map when it is a map
  • aname_dict for larger more complex structure.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpokstadView Question on Stackoverflow
Solution 1 - PythonDSblizzardView Answer on Stackoverflow
Solution 2 - PythonviraptorView Answer on Stackoverflow
Solution 3 - PythonSklavitView Answer on Stackoverflow
Solution 4 - PythonunutbuView Answer on Stackoverflow
Solution 5 - PythonTerrabitsView Answer on Stackoverflow
Solution 6 - PythonIgnacio Vazquez-AbramsView Answer on Stackoverflow
Solution 7 - PythonL. G.View Answer on Stackoverflow