What Is a Dictionary in Python?
Dictionaries are Python’s implementation of a data structure that is more generally known as an associative array. A dictionary consists of a collection of key-value pairs. Each key-value pair maps the key to its associated value.
You can define a dictionary by enclosing a comma-separated list of key-value pairs in curly braces ({}
). A colon (:
) separates each key from its associated value:
d = {
<key>: <value>,
<key>: <value>,
.
.
.
<key>: <value>
}
00:00 Hey there! Thanks for joining me on this course about dictionaries in Python. In this course, we’ll define dictionaries—a very handy datatype—and we’ll talk about incrementally building dictionaries, something that you’ll do often.
00:17 We’ll talk about restrictions on keys and values. And finally, we’ll cover some common operators and methods that will take your dictionary usage to the next level.
00:29 So, let’s get started with “What is a dictionary?” A dictionary is a set of key-value pairs, with the keys being unique within the dictionary. This makes the dictionary useful for storing and retrieving values using that unique key.
00:48 A simplified example of this could be a profile on a social media platform. Your profile is attached to some sort of unique identifier, and that can be used to retrieve data from your profiles—like your hometown, your birthday, your list of friends, and so on. Some traits of dictionaries: The objects within a dictionary can be edited or changed.
01:08 This is known as being mutable. They are dynamic. Dictionaries can grow or shrink as needed. And they can be nested. A dictionary can contain other dictionaries or lists as needed.
01:23 So, syntax here—a dictionary contains its key-value pairs within curly braces. So you can see, typically you will name your dictionary and then you’ll open it up and close it with curly brackets that contain your key-value pairs.
01:40
The best way to get to know dictionaries is to work with them, so go ahead and open up your Python console and we’ll work through some examples. Let’s go ahead and define a dictionary and put some comic book bad guys in there. So we name our dictionary, go ahead and open it up, beginning curly bracket ({
).
02:06
We’ll start working on our key-value pairs. So, Daredevil’s bad guy is Kingpin. And remember that we separate key-value pairs with a comma (,
).
02:17 Let’s add a couple more here.
02:28 Close off our dictionary. And you can simply call your dictionary by typing in the dictionary name, hitting Enter, and it’ll give you your key-value pairs right there.
02:40 A quick note here that as of Python version 3.7, dictionaries will display in the order in which they were defined. This behavior does not exist in earlier versions of Python. It may return a random order.
02:55
You can retrieve a value from a dictionary by specifying its key in square brackets, and it looks like this. Let’s get the bad guy from 'batman'
, 'x-men'
would be 'apocalypse'
.
03:16 And if you specify a key that is not in the dictionary, you’ll get an exception. So remember that, and we’ll work on some ways around that later in the course.
03:29
Adding an entry to your dictionary is just a matter of assigning a new key and a value. So if we added 'deadpool'
as our key and I want to add in the bad guy,
03:48
and then list the dictionary, we can see that if I scroll over here, 'deadpool'
has been added. And if you ever want to update an entry, you just need to assign a new value to the key.
04:06
And let’s switch it to 'juggernaut'
.
04:15
And you can see 'juggernaut'
is now in our dictionary. To delete an entry, we’ll just use the delete del
statement and specify the key.
04:28
And 'x-men'
will be gone. Goodbye 'juggernaut'
. Something else to note when you’re working with dictionaries—and this can be confusing when you’re starting out—if you’ve worked with lists, you’ll know that you can access list items with an index number.
04:45 You cannot treat dictionaries as a list. You won’t be able to access values with an index number, like so. You’ll get an exception. This is because Python is expecting a key. Related to that point, you can use integers as keys.
05:03 So if I start a new dictionary here,
05:10 you can see that we’re now using integers as the key.
05:18
And in this case, we’ll be able to access values in the dictionary with a 0
or a 1
, because they are keys—not an index. That may be something you see in code, and it might be confusing to see, but know that they’re specifying a key in a dictionary and not an index.
kiran on Aug. 9, 2020
can you drop your slide here…?
Mariano Bello on Jan. 8, 2023
Great introduction Paul. Concise and informative.
One comment though: in the part where you define your dicitionary as:
your_dictionary = {
key: value,
key value
}
I would suggest to use something like this instead:
your_dictionary = {
key1: value1,
key2: value2
}
Since moments before introducing this example you talk about the uniqueness of the keys.
It should be noted that value1
can be the same as value2
but key1
can’t be the same as key2
Bartosz Zaczyński RP Team on Jan. 9, 2023
@Mariano Bello These are all good observations. Thank you!
Become a Member to join the conversation.
Harsha Vardhan on Jan. 2, 2020
From Python 3.7, Dictionary order is guaranteed to be insertion order.
Before Python 3.6, we have to use OrderedDict from collections module.
Am i correct here ?
Thank you Paul