About this video
A dictionary stores data in key-value pairs, so we access a value by a meaningful key instead of an index. Dictionaries are defined with curly brackets, accessed and updated with square brackets, and are logically unordered. Keys must be unique and immutable, lookup only goes from key to value, the in operator checks keys, and the format is so flexible that most internet data uses the similar JSON format.
You'll be able to
- define a dictionary and access a value by its key.
- add and update items in a dictionary.
- explain why keys must be unique and immutable while values need not be.
- use the in operator to check whether a key exists.
Key words
- dictionary
- A data structure that stores data as key-value pairs.
- key
- The unique label used to look up a value in a dictionary.
- value
- The data stored with a key; values don't have to be unique.
- item
- One key-value pair in a dictionary.
- KeyError
- The error you get when you look up a key that isn't in the dictionary.
- JSON
- JavaScript Object Notation, a dictionary-like format used for most data sent around the internet.
Transcript
- [Instructor] So you have a bunch of related data. You could store them all in separate named variables, but now you have a bunch of variables to manage. Or you could store them all together in a list, but then you need to remember which value is it, which index.
What if we could have the best of both worlds? We store all the data in one place, but we attach a meaningful label to each value. In Python, we call this data structure a dictionary. A dictionary stores data in key value pairs. Instead of accessing a value based on its position or index, we access a value based on its key.
So for example, we might look up the value associated with the key duration. To define a new dictionary, we use curly brackets. So here we have an empty dictionary named video. For each key value pair, we put the key, a colon, and then the value. So this item has the key title and the value user input.
We separate the items of a dictionary with commas. So this dictionary has five items or five key value pairs. We can also count the number of items using the built-in length function. In Python, technically the computer does maintain the order in which the items are defined.
However, logically, dictionaries are unordered data structures. The ordering of the key value pairs doesn't hold a particular meaning for the computer, so this dictionary and this dictionary are equivalent. We define a dictionary using curly brackets, but to access values we still use square brackets.
We put the name of the dictionary, square brackets, and the key we want to access, which tells the computer to look up the value associated with that key. If the dictionary doesn't have an item with that key, we get a key error. Note that the lookup only goes in one direction.
We can look up values using their keys, but we can't look up keys using their values. This means that keys must be unique, but values don't have to be. Dictionaries are mutable, so we can add and update items after the dictionary is defined. We write an assignment statement with the dictionary name, square brackets, and the key of the item we want to update.
On the right hand side then, we put the updated value. If the key doesn't already exist in the dictionary, this adds a brand new item with this key and this value. If the key does already exist, this overwrites the key's current value because we can't have two items with the same key.
How do we know which views value to use? We can check if a key already exists in a dictionary using the in operator. The in operator only checks against the dictionary's keys. It does not search the dictionary values. We can store any type of data we want in a dictionary.
Dictionary values can be strings, integers, floats, bullions, lists, even other dictionaries. The keys also don't have to be strings. They just need to be unique and an immutable data type. So we might also have dictionaries with integer keys. Dictionaries are a simple yet enormously powerful way to organize data.
They can represent basic relationships between pieces of data like usernames and passwords or products and prices. They can also represent any kind of structured data record where the keys serve as labels for the individual data fields. And because this key value structure is so flexible, yet easy to understand, most of the data passed around the internet is represented in a dictionary like format called JSON or JavaScript Object Notation.
So no matter what data you're trying to represent, if the ordering or sequence of your data isn't relevant, you can likely solve that problem with a dictionary. You store the key value pairs and you look up values based on their keys.
Video © Khan Academy, used under CC BY-NC-SA. Watch on Khan Academy. Puzzicle is not affiliated with Khan Academy.