Intro
This tutorial will teach you about the dictionary data-structure in Python.
Requirements
- Know basics of Python 3 if not read here
- Knowledge of Computer Science data-structures will help
What are they?
A dictionary is an ordered collection of key-value pairs. Each key added to a dictionary has to be unique, the value assigned to a key does not have to be unique. In Python a dictionary’s key can be any object (objects will be covered in a later tutorial) that is marked “hashable”. For example they could be (but not limited to) an int, string or float data-type. The keys can also be of different data-types inside one dictionary.
Creation
First let’s start off by creating a dictionary. Our first dictionary will be simple; it will have a players name as the key and a score (int) as the value.
To create a dictionary we first have to assign it to a variable, in our case it will be called “player_scores”. We then use curly braces to define we are making a dictionary. In our dictionary we will have 3 players these players will be called “Bob”, “Steve” and “Dave”. As you can see our dictionary keys will be strings. Then to create the key-pair we use the colon character to give a value to our key. When we have multiple key-value pairs we use a comma to separate each one.
|
|
When our program is run will will get the dictionary output. This is shown below:
|
|
Accessing Elements
We can access each value in a dictionary by its key, we can achieve this similar to how we access lists.
As you can see from the code below we can access a value by using square brackets, like we do when accessing an index in a list, however this time instead of passing an index we use a value that is a key in the dictionary.
|
|
There are multiple other ways to access a value inside a dictionary. For example using the .get() function
When this is run we should get the output of: 12
.
Modifying Elements
Also like a list we can modify a value in a dictionary by using square brackets, and then instead of an index we use a key. Like shown below:
|
|
When this is run we should see that the “Dave” key is now equal to 100.
|
|
Adding Elements
We can also add a new key-value pair to the dictionary. You may notice that we are using the same statement as modifying a element. However in this case we know that that key-value pair doesn’t exist.
|
|
When the code above is run we get the following output:
|
|
We can see that it has however added a new key-value pair to the dictionary.
Removing Elements
Let’s delete an element from the dictionary. We can delete elements several ways. Like a list we can use “.pop” and the “del” keyword.
Del
This example will show you how to delete an element using the “del” keyword.
|
|
As you can see you simply place the “del” keyword when accessing the player_scores dictionary by a key. When this is run you should get the following output:
|
|
There are other ways of deleting key-value pairs not covered in this tutorial.
Clear
A dictionary has a special function that is not included with a list, this method will allow us to clear a dictionary of all key-value pairs.
|
|
As you can see when you run this code you will end up with an empty dictionary. You will get the following output:
|
|
Iteration
We can also iterate over the keys in a dictionary.
Iteration is covered in another tutorial here.
|
|
As you can see when run we get each key. We can then access the value by using the method shown before in the tutorial.
|
|
Extras
Dictionaries in Python come with some useful functions we can call on the object. Here I will show extracting the keys and values.
Looking at this code you may notice .keys()
and .values()
are being converted into a list, this is because Python returns an iterable that cannot be accessed by index; so I am converting them into lists.
Try removing the list conversion to see what happens
|
|
As you can see from the output we have the keys, values and the first key displayed.
|
|
Research
There is much more to learn about dictionaries, have a look at the official Python documentation for more in-depth knowledge here.