SoFunction
Updated on 2024-11-18

An overview of what types of Python's dictionary key names can be

One thing I noticed today while looking at someone else's code was that it was treating objects as dictionary keynames and had two objects (instances of classes) as keynames, then went and looked it up:

Keys must be immutable, such as strings, numbers, or tuples.

1 Type of key, list/dictionary not allowed, everything else is allowed

But the internet doesn't say if and how other types can be used. I wrote code to try it:

class Person:
    def __init__(self, name):
         = name

i = 5
s = 'abc'
t = (5,'a')
p = Person('Lily')
q = Person('xiao')
m = {'a':1, 'b':10}
lst = [1,2,3]

d = {}
d[i] = 'five'
d[s] = 'ABC'
d[t] = 'five-a'
d[p] = 'name:Lily'
# d[lst] = 'list : 1,2,3'
# TypeError: unhashable type: 'list'
d[p, q] = 'two people: Lily and xiao'
d[i,s,t,p,q] = 'all in key'

for k, v in ():
    print(k, '=>', v)

Output results:

5 => five
abc => ABC
(5, 'a') => five-a
<__main__.Person object at 0x000001803EEF27F0> => name:Lily
(<__main__.Person object at 0x000001803EEF27F0>, <__main__.Person object at 0x000001803EEF28D0>) => two people: Lily and xiao
(5, 'abc', (5, 'a'), <__main__.Person object at 0x000001803EEF27F0>, <__main__.Person object at 0x000001803EEF28D0>) => all in key

2 Multiple objects can be treated as key names, with different keys in different orders.

print(d[p, q])
print(d[q, p])

Output:

two people: Lily and xiao
Traceback (most recent call last):

  File "<ipython-input-15-12aff481ab93>", line 1, in <module>
    runfile('C:/Users/Xpeng/.spyder-py3/', wdir='C:/Users/Xpeng/.spyder-py3')

  File "D:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\spyder\utils\site\", line 705, in runfile
    execfile(filename, namespace)

  File "D:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\spyder\utils\site\", line 102, in execfile
    exec(compile((), filename, 'exec'), namespace)

  File "C:/Users/Xpeng/.spyder-py3/", line 37, in <module>
    print(d[q, p])

KeyError: (<__main__.Person object at 0x000001803EF58940>, <__main__.Person object at 0x000001803EF58668>)

3 Conclusion [in error]:

(1) Except for the list, which cannot be used as a key name, everything else can be used, and you can put more than one.
(2) The way I understand it, lists are mutable, all other types are immutable. When an object is used as a key name, what is actually passed in is the address of the object, which is also immutable.
(3) The keys are different when you put more than one in a different order.

------2020.04.07 Updated -----
Thanks for the two-time lurker reminder.
(1) It is accurate to say that lists, dictionaries and other non-hashable types can not be used as keys, and only hashable types can be used as keys.

to this article on the Python dictionary key names can be what type of article is introduced to this, more related Python dictionary key names content please search my previous articles or continue to browse the following related articles I hope you will support me in the future more!