Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Creating Dictionaries

00:00 In this lesson, I’ll show you how to create a dictionary in Python. Python dictionaries, like lists and tuples, store a collection of objects. However, instead of storing objects in a sequence, dictionaries hold information in pairs of data called key-value pairs.

00:17 The key in a key-value pair is a unique name that identifies the value part of the pair. For example, you could use a dictionary to store the names of states and their capitals.

00:29 In this table, the keys of the dictionary are the names of the states, and the values of dictionary are the names of their capitals.

00:37 In Python, this dictionary would look like this. A dictionary in Python is enclosed in curly braces ({}). Each key is separated from its value by a colon (:), and each key-value pair is separated by a comma (,).

00:52 Let’s hop over to the IDLE Shell to explore this data structure a bit more.

01:00 Again, a dictionary is enclosed by curly braces. So when I type capitals = {}, I just created a dictionary. To validate that capitals is a dictionary, you can check its type with type(capitals).

01:21 So this just was a empty dictionary. Let’s create a new dictionary with the three capitals from the example before. So again, let’s write capitals = { and then "California": "Sacramento",

01:42 "New York": "Albany",

01:47 "Texas": "Austin",

01:52 and closing with a } again.

01:55 So there you see all the key-value pairs. The key "California" has the value of "Sacramento", and the key "New York" has the value "Albany", and the key "Texas" has the value "Austin".

02:07 All of them are strings. With this code, you created a dictionary literal containing names of states and their capitals. You can also create a dictionary from a sequence of tuples using the built-in dict() function. Let’s create a sequence of tuples first.

02:24 This time, let’s get a bit more personal and use my dog Frieda in this example. I name this tuple my_dog, and this tuple contains four inner tuples.

02:37 The first one is ("name", "Frida"). The second one, ("age", 5). That’s an integer 5. The third one, ("nicknames", and then let’s create a list. The first nickname of Frieda is "Fru-Fru", and the second nickname is "Lady McNugget".

02:59 And the fourth item of the tuple list is ("hungry", True). So that’s a Boolean here. So as you can see, we have a tuple object containing four inner tuples that not only contain strings as the second item in the tuple, but also an integer, a list, and a Boolean.

03:23 When you pass in my_dog as an argument for the dict() function, then Python creates a dictionary out of the sequence of tuples.

03:33 In the example of the capitals, all values were strings, but here the values are also an integer, a list, and a Boolean. This has nothing to do with the dict() function that you used.

03:43 That’s a feature of dictionaries in general. Values of dictionaries can have any valid Python type. Okay, but what about keys then?

03:55 So far, all the dictionary keys we’ve worked with were strings. However, there is no rule that dictionary keys must be all strings. Even more, they don’t even have to be the same type. You can use strings, integers, floats, Booleans, and tuples as dictionary keys.

04:12 And there are some more data types that you can use as dictionary keys, but as this as a Python Basics course, let’s stay basic for now. One thing that all these data types have in common is that they are immutable.

04:26 That means once they are created, you cannot change the value of an element that they contain. So immutable data types are allowed as keys. Not allowed are mutable data types like lists and dictionaries as keys for Python dictionaries.

04:44 If you try to create a dictionary with a mutable type like a list, then Python will throw a TypeError and say that your key is not hashable.

04:53 In other words, Python must know exactly what to look for when you are accessing a dictionary … which is a great segue to the next lesson. In this lesson, you learned how to create a dictionary and which data types are allowed to be used as keys and values. In the next lesson, you’ll learn how to access dictionary values.

Become a Member to join the conversation.