Dictionaries
Dictionary is a collection of key-value pairs. Each key is connected to a value, and you can use a key to access the value associated with that key.
Creating a Dictionary
To define a dictionary, you use curly braces {}
to indicate the beginning and end of the dictionary, and use :
to separate keys and values. A simple dictionary looks like this:
languages = {'Python': 'Readable', 'C++': 'Fast', 'Java': 'Reliable', 'Rust': 'Safe'}
print(languages)
which produces the following output:
{'Python': 'Readable', 'C++': 'Fast', 'Java': 'Reliable', 'Rust': 'Safe'}
Note that after Python 3.6, the order of the key-value pairs is preserved. In earlier versions of Python, the order of the key-value pairs is not preserved.
Also note that the keys in a dictionary must be unique. If you use the same key twice, the second key-value pair will overwrite the first one.
Accessing Values
To get the value associated with a key, give the name of the dictionary and then place the key inside a set of square brackets []
. For example, the following snippet
print(languages['Python'])
print(languages['C++'])
print(languages['Java'])
print(languages['Rust'])
produces an output:
Readable
Fast
Reliable
Safe
If we try to access a key that doesn't exist in a dictionary
print(languages['C'])
Python will raise an error:
print(languages['C'])
~~~~~~~~~^^^^^
KeyError: 'C'
If this is an issue, there's another way to access a value in a dictionary. The get()
method returns None
if the key doesn't exist in the dictionary. You can also specify a default value to use if the key doesn't exist.
print(languages.get('Python')) # same as languages['Python']
print(languages.get('C') is None) # key "C" does not exist
print(languages.get('C', 'Fun')) # returns "Fun"
If you need to check whether dictionary has a particular key, you can use the in
operator.
print('Python' in languages) # True
print('C' in languages) # False
Looping Through a Dictionary
It's very common to loop through all key-value pairs in a dictionary.
Below is an example of using for
loop. Notice that the language
variable gets the successive keys in the dictionary as the loop moves through the dictionary.
for language in languages:
print(f"{language} is {languages[language].lower()}")
And this the output:
Python is readable
C++ is fast
Java is reliable
Rust is safe
Because it's common to use both key and value of a dictionary while looping through it, Python provides a special method called items()
that returns a list of key-value pairs. In the following example, we use items()
to loop through the dictionary and do the same thing as the previous example.
for language, quality in languages.items():
print(f"{language} is {quality.lower()}")
The keys()
method returns a list of all keys in a dictionary. The values()
method returns a list of all values in a dictionary. These two methods can be used in variour ways. For example, you can loop through keys in sorted order:
for language in sorted(languages.keys()):
print(f"{language} is {languages[language].lower()}")
Which produces almost the same output as the previous examples, except that the languages are now sorted:
C++ is fast
Java is reliable
Python is readable
Rust is safe
Change Dictionaries
It's fairly easy to change dictionaries. You can add new key-value pairs, modify existing key-value pairs, and remove key-value pairs.
The following example creates an empty dictionary and adds key-value pairs to it. In tnis case we add names of students as keys and their yearly points as values.
students = {}
students['Alice'] = 20 # {'Alice': 20}
students['Bob'] = 19 # {'Alice': 20, 'Bob': 19}
Another way to add or change key-value pairs in a dictionary is to use the update()
method. The update()
method takes a dictionary as an argument and adds all key-value pairs from that dictionary to the current dictionary. If the key already exists, the value associated with that key is replaced.
students.update({'Alice': 22, 'Charlie': 25, 'Daniel': 23})
If we print students
dictionary now, we'll see that the argument dictionary was added to the students
dictionary.
{'Alice': 22, 'Bob': 19, 'Charlie': 25, 'Daniel': 23}
If we need to remove items from a dictionary, there are few ways to do it. The popitem()
mehtod removes the last key-value pair from a dictionary and returns it as a tuple.
print(students.popitem())
# => ('Daniel', 23)
The pop()
method removes a key-value pair from a dictionary and returns the value associated with the key. If the key doesn't exist, the method returns the default value specified as the second argument (or fails with error).
print(students.pop('Bob', 33)) # => 19 (Bob's points)
print(students.pop('Bob', 33)) # => 33 (default value)
The del
statement removes a key-value pair from a dictionary.
del students['Charlie']
And, finally, the clear()
method removes all key-value pairs from a dictionary.