Python dict key naming convention

Python dict key naming convention

Pino

I (infrequently) blog about data science, business intelligence, big data, web technologies and free software.

LinkedIn

Yesterday, I realised that I have never shared any gist on github. Since it’s about time to start, I did it with the very first script that I wrote today.

I adapted two scripts contributed to StackOverflow and created a new script that converts the keys of nested Python dictionaries to a different naming convention. These are the scripts that I used:

  • How to recursively replace the keys of a Python dictionary, which I modified to do the same with nested dictionaries that contain lists

  • How to convert from camel case naming convention to underscore lowercase naming convention and vice versa.

And this is the result:


Do you name a dictionary by the key or the value? I have a dictionary that will hold gradient data and the component:

dictionary[gradient] = component;

Do I name it gradientDictionary or componentDictionary?

asked Apr 5, 2017 at 9:37

Python dict key naming convention

1.21 gigawatts1.21 gigawatts

1,1991 gold badge9 silver badges20 bronze badges

2

That depends, but generally I'd suggest naming by the values. The keys are often just a way to quickly find what you are actually looking for and will be using.

Depending on context, for your specific example, I'd also probably suggest just calling the dictionary components to avoid encoding too much type information in the name.

answered Apr 5, 2017 at 10:00

1

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 Naming Conventions for DictionariesMapsHashes

Questions : Python Naming Conventions for DictionariesMapsHashes

2022-09-27T12:13:31+00:00 2022-09-27T12:13:31+00:00

687

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

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

Some naming convention samples I have been anycodings_dictionary 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 anycodings_dictionary "because my work tells me to", not very anycodings_dictionary helpful. The reason driving the choice is anycodings_dictionary more important. Are there any other good anycodings_dictionary considerations for a dictionary naming anycodings_dictionary convention aside from readability?

EDIT:

Chosen answer: value_key_map

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

Total Answers 7

28

Answers 1 : of Python Naming Conventions for DictionariesMapsHashes

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

0

2022-09-27T12:13:31+00:00 2022-09-27T12:13:31+00:00Answer Link

mRahman

4

Answers 2 : of Python Naming Conventions for DictionariesMapsHashes

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

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

0

2022-09-27T12:13:31+00:00 2022-09-27T12:13:31+00:00Answer Link

jidam

3

Answers 3 : of Python Naming Conventions for DictionariesMapsHashes

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

0

2022-09-27T12:13:31+00:00 2022-09-27T12:13:31+00:00Answer Link

raja

3

Answers 4 : of Python Naming Conventions for DictionariesMapsHashes

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

0

2022-09-27T12:13:31+00:00 2022-09-27T12:13:31+00:00Answer Link

joy

2

Answers 5 : of Python Naming Conventions for DictionariesMapsHashes

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

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

To answer the question of what I DO use:

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

0

2022-09-27T12:13:31+00:00 2022-09-27T12:13:31+00:00Answer Link

joy

2

Answers 6 : of Python Naming Conventions for DictionariesMapsHashes

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

0

2022-09-27T12:13:31+00:00 2022-09-27T12:13:31+00:00Answer Link

raja

6

Answers 7 : of Python Naming Conventions for DictionariesMapsHashes

In our projects, we adopted following anycodings_mapping convention:

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

0

2022-09-27T12:13:31+00:00 2022-09-27T12:13:31+00:00Answer Link

jidam

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, : .

What would be another good name for a dictionary Python?

They are Python's built-in mapping type. They map keys, which can be any immutable type, to values, which can be any type, just like the values of a list or tuple. Other names for dictionaries in computer science include maps, symbol tables, and associative arrays.

What does dict [] do in Python?

The dict() function creates a dictionary. A dictionary is a collection which is unordered, changeable and indexed.

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.