Skip to content

Dictionaries and Tuples

Dictionaries and Tuples

Besides lists, Python has two additional data structures that can store multiple objects. These data structures are dictionaries and tuples. Tuples will be discussed first.

Tuples

Tuples are immutable lists. Elements of a list can be modified, but elements in a tuple can only be accessed, not modified. The name tuple does not mean that only two values can be stored in this data structure.

Tuples are defined in Python by enclosing elements in parenthesis ( ) and separating elements with commas. The command below creates a tuple containing the numbers 3, 4, and 5.

>>> t_var = (3,4,5)
>>> t_var
(3, 4, 5)

Note how the elements of a list can be modified:

>>> l_var = [3,4,5]  # a list
>>> l_var[0]= 8
>>> l_var
[8, 4, 5]

The elements of a tuple can not be modified. If you try to assign a new value to one of the elements in a tuple, an error is returned.

>>> t_var = (3,4,5)  # a tuple
>>> t_var[0]= 8
>>> t_var

TypeError: 'tuple' object does not support item assignment

To create a tuple that just contains one numerical value, the number must be followed by a comma. Without a comma, the variable is defined as a number.

>>> num = (5)
>>> type(num)
int

When a comma is included after the number, the variable is defined as a tuple.

>>> t_var = (5,)
>>> type(t_var)
tuple

Dictionaries

Dictionaries are made up of key: value pairs. In Python, lists and tuples are organized and accessed based on position. Dictionaries in Python are organized and accessed using keys and values. The location of a pair of keys and values stored in a Python dictionary is irrelevant.

Dictionaries are defined in Python with curly braces { }. Commas separate the key-value pairs that make up the dictionary. Each key-value pair is related by a colon :.

Let's store the ages of two people in a dictionary. The two people are Gabby and Maelle. Gabby is 8 and Maelle is 5. Note the name Gabby is a string and the age 8 is an integer.

>>> age_dict = {"Gabby": 8 , "Maelle": 5}
>>> type(age_dict)
dict

The values stored in a dictionary are called and assigned using the following syntax:

dict_name[key] = value
>>> age_dict = {"Gabby": 8 , "Maelle": 5}
>>> age_dict["Gabby"]
8

We can add a new person to our age_dict with the following command:

>>> age_dict = {"Gabby": 8 , "Maelle": 5}

>>> age_dict["Peter"]= 40
>>> age_dict
{'Gabby': 8, 'Maelle': 5, 'Peter': 40}

Dictionaries can be converted to lists by calling the .items(), .keys(), and .values() methods.

>>> age_dict = {"Gabby": 8 , "Maelle": 5}

>>> whole_list = list(age_dict.items())
>>> whole_list
[('Gabby', 8), ('Maelle', 5)]

>>> name_list = list(age_dict.keys())
>>> name_list
['Gabby', 'Maelle']

>>> age_list = list(age_dict.values())
>>> age_list
[8, 5]

Items can be removed from dictionaries by calling the .pop() method. The dictionary key (and that key's associated value) supplied to the .pop() method is removed from the dictionary.

>>> age_dict = {"Gabby": 8 , "Maelle": 5}
>>> age_dict.pop("Gabby")
>>> age_dict
{'Maelle': 5}