Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Restrictions on Dictionary Keys and Values

Give Feedback

Almost any type of value can be used as a dictionary key in Python. You can even use built-in objects like types and functions. However, there are a couple restrictions that dictionary keys must abide by.

First, a given key can appear in a dictionary only once. Duplicate keys are not allowed. A dictionary maps each key to a corresponding value, so it doesn’t make sense to map a particular key more than once. If you specify a key a second time during the initial creation of a dictionary, then the second occurrence will override the first.

Second, a dictionary key must be of a type that is immutable. For example, you can use an integer, float, string, or Boolean as a dictionary key. However, neither a list nor another dictionary can serve as a dictionary key, because lists and dictionaries are mutable. Values, on the other hand, can be any type and can be used more than once.

00:00 You’ve seen so far that dictionaries are very flexible, kind of an awesome data type to work with. But there are a few restrictions I want to briefly talk about. Keys in a dictionary can only be used once. If it is used more than once, as you saw earlier, it’ll simply replace the value.

00:19 A key must be immutable—that is, unable to be changed. These are things like integers, floats, strings, Booleans, functions. Even tuples can be a key. A dictionary or a list cannot be a key. Values, on the other hand, can literally be anything and they can be used more than once.

00:41 Let’s hop into the console for a couple of examples. You’ve seen so far that you can use values of all sorts in a dictionary. The same goes for keys. You don’t have to simply use strings or integers—you can use floats, Booleans, even tuples as keys.

01:00 If we open up an example dictionary here and let’s give it a couple of keys of those types.

01:13 There’s a Boolean. And let’s try with a tuple here.

01:25 Go ahead and close this dictionary off. And you can see it took those without exception. And even call one of those—no problem! So, remember the restrictions we talked about.

01:41 They can only be used once in a dictionary and keys must also be immutable. So, what happens if we use one of those mutable data types? Like a list as a key?

02:02 And try to assign it? Oops, I have plural examples. So you can look here, and our second exception that we got—unhashable type: 'list'.

Become a Member to join the conversation.