Introduction to Python Dictionaries

Erika D
6 min readNov 21, 2019

--

What is A Python Dictionary

A Python dictionary is a data type that holds a collection of key-value pairs. A dictionary is similar to a list, but instead of each element containing a single value, each element is a key-value pair. Another difference between a dictionary and a list is that the contents of a dictionary are wrapped in curly braces {}, whereas the contents of a list are wrapped in brackets [ ]. A key-value pair is separated by a colon (:) and each key-value pair is separated by a comma. The format of a dictionary can be seen below:

How to Create a Dictionary in Python

To create an empty dictionary, you simply assign a variable name to a pair of curly braces { }.

When we check the data type of my_dict we can see that python recognizes my_dict as a dict aka, a dictionary.

To create a dictionary that contains key-value pairs, you would simply insert the key-value pairs between the curly braces. Each element in the dictionary must have a key and a corresponding value. Key-value pairs are separated by a colon ( : ) and each key-value pair is separated by a comma ( , ). The following dictionary maps the name of a child to their corresponding age:

To create a dictionary, you may also use the built-in dict( ) function. You would insert a list of tuples, each containing a key-value pair, within the function. The below dictionary is created with the dict( ) function

How to Access Elements in a Dictionary

To access a value from within a dictionary, you clarify what dictionary you are accessing data from and then wrapped in brackets [ ], the name of the key from the dictionary, dictionary_name[key]. To see how old Carly is, we would type the name of the dictionary with the names and ages of children, my_dict, and then specify the key name we want to retrieve the value for, ‘Carly’, inside brackets.

If you try to retrieve a key that is not in the dictionary, python will raise an error.

To access a value from a dictionary you can also use the get() method. The get method will return the value of a specified key from a dictionary. To retrieve Carly’s age from the my_dict dictionary using the get() method, the following code would be used:

Deleting Elements in a Dictionary

To remove an element from a dictionary, we can use the pop() method. The pop() method will remove a specified key-value pair from a dictionary. The format for the pop() method is, dictionary_name.pop(key). The pop() method will return the value of the key you are removing from the dictionary.

When we return our dictionary, we will see that ‘Alex’ is no longer included.

The popitem( ) function removes the last key-value pair in a dictionary. We can see above that the last key-value pair in the my_dict dictionary is ‘Evan’ : 6. The popitem( ) function will return ‘Evan’ : 6 and remove it from the my_dict dictionary.

When we print the my_dict dictionary, you will see that ‘Evan’ : 6 is no longer in the dictionary.

The del keyword can also be used to remove an item from a dictionary. The format to delete an item from a dictionary using del is, del dictionary_name[key]. No value is returned when using the del method.

When you print the my_dict dictionary, you will see that ‘Ben’ : 10 is no longer in the dictionary.

We can see that ‘Ben’ is no longer included in the my_dict dictionary. If we wanted to delete all the items in a dictionary, we could use the clear() function. It will remove all key-value pairs from a dictionary.

To check that the dictionary is empty, we will call the dictionary.

To delete a dictionary entirely you can use the del keyword followed by the dictionary name.

When you print the dictionary, my_dict, Python will raise an error since the dictionary no longer exists.

Adding Elements to a Dictionary

Since we just deleted the dictionary, my_dict we will run the code to create the dictionary used earlier.

To add an item to a dictionary you would use the assignment operator and the format dictionary_name[key] = value. The following code adds another child, ‘Sam’, to the dictionary, who is age 10.

We can see that ‘Sam’ is now included in the dictionary. To add a key-value pair to a dictionary, you can use update( ) function. The below code adds, Fred, age 8 to the my_dict dictionary.

Notice that Fred is not wrapped in quotation marks when using the update( ) function. When we return the dictionary we can see that Fred has been added to the my_dict dictionary.

Updating Elements in a Dictionary

To update the value of an existing key within a dictionary you would simply assign the key a new value. To do this the format is, dictionary_name[key] = new value. To change Danielle’s age from 7 to 8, we could use the following code:

To update an existing key in a dictionary, the update( ) function can also be used. The below code will update Ben’s age to 11 and Danielle’s age to 9.

Viewing Items, Keys and Values in a Dictionary

To view each key-value pair in a dictionary you can use the .items( ) method.

To view all the keys in a dictionary you can use the .keys( ) method.

To view all the values in a dictionary you can use the .value( ) method.

This was a short introduction to a few basic dictionary functions. Keep practicing creating and modifying your own python dictionaries to understand the methods discussed in this blog better.

--

--