68

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.

Community
  • 1
  • 1
pokstad
  • 3,411
  • 3
  • 30
  • 39
  • 8
    IMHO, many people with some math background may find `value_key_map` quite confusing as it may suggest that the dictionary maps values to keys (i.e. an inverted dictionary) which obviously is not intended. – Loax Jan 11 '15 at 05:11
  • 3
    In python parlance, `dict` is the correct term to use. (`map` is a function https://docs.python.org/3/library/functions.html#map.) It would be better to name as `key_value_dict`, so for example `id_user_dict` has `id` as the key and `user` as the value. – arun Jun 11 '15 at 18:01
  • 2
    What if underscores already exists in the names? e.g. `item_id_name_age_map`, can you guess what are key and value? – Tengerye May 11 '20 at 04:03

7 Answers7

39

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.

DSblizzard
  • 4,007
  • 7
  • 48
  • 76
  • 4
    This is my approach too. The *_to_* naming convention quickly indicates that it's a dict, so no need for *_map. It also gives a nice little calculus to read. `apple = oranges_to_apples[my_orange]`. – Matthew Cornell Nov 18 '15 at 13:12
  • 6
    I recently came across a similar question for C# where [an answer](http://stackoverflow.com/a/4330705/945456) suggested `values_by_key` which seems like another good option. – Jeff B Mar 15 '17 at 14:04
  • Similar here. But instead of "oranges_to_apples[my_orange]" I would use "apple_per_orange[my_orange]" or "tel_per_person[Mueller]" – gebbissimo Jan 19 '19 at 12:47
  • 1
    Seem to be in the minority here, but e.g. `salary_from_joe = surname_to_salaray["joe"]` doesn't sound very English to me compared to `salary_by_surname["joe"]` or `salary_per_surname["joe"]`. In other words, I would argue in favor of `value_by_key` instead of `key_to_value`. – gebbissimo Jul 14 '21 at 13:23
  • 1
    "The _to_ naming convention quickly indicates that it's a dict" To me it sounds like a function, not a dict – agemO Jan 19 '22 at 13:08
16

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".

viraptor
  • 33,322
  • 10
  • 107
  • 191
  • 5
    Why not key_value_map? It is more natural, just search for "key_value_map" and "value_key_map" (with quotes) in google. – DSblizzard Mar 21 '15 at 10:18
  • @DSblizzard Honestly, I think it was just accidental because I saw @pokstad listing value in the first position in his proposals - a bit of subconscious suggestion. `key_value_map` is probably what I would use in my code too. – viraptor Mar 23 '15 at 23:57
10

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
Sklavit
  • 2,225
  • 23
  • 29
6

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.

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • 1
    That was my first instinct, but I feel like the naming convention between my lists and my dictionaries are too similar. When I come back to revisit old code I constantly have to find where something is initialized or accessed by key so that I know my dictionary is not a list or vice versa. – pokstad Mar 09 '10 at 15:56
  • 2
    @pokstad: Yes, I see what you're saying. However, if you look at the standard library, its dicts seem to be named after the `values`only, never the more wordy `values_key_map`. For example, `sys.modules`, or `locals()`. That seems to be the way at least the developers think is best. Perhaps a bit of documentation could help too. – unutbu Mar 09 '10 at 18:29
  • But it is not enough if you are inspecting someone else's code and try to understand what dictionary `values` is for. – Sklavit Jul 30 '21 at 10:46
  • I think this answer is great when you have nested dictionaries. If you have e.g. balances saved by person and invoice number, then each use of the dictionary is just balance[person][invoice]. Compare to balance_by_person_invoice[person][invoice], for example. I think for regular dicts this reads well too, e.g. balance[personID].. and most written code in the usage of the dictionary, not its definition. – doublefelix Jul 17 '22 at 19:09
5

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.

Terrabits
  • 997
  • 2
  • 15
  • 19
3

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.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • 1
    +1 I like this naming convention because it makes it known very quickly that you are dealing with a dictionary and not a list. The only thing I don't like is that the entire word map seems like overkill, is there a popular standard that accomplishes this with some sort of shorthand for 'map'? Is there a Pythonic way to remind the code reviewer that the object is a map? – pokstad Mar 09 '10 at 16:01
2

In our projects, we adopted following convention:

  • key_to_value_map when it is a map
  • aname_dict for larger more complex structure.
L. G.
  • 9,642
  • 7
  • 56
  • 78