Python - Dictionary of Lists


Python - Dictionary of Lists

What is a dictionary of lists in Python? A dictionary in which the value for each key is a list.

For example, the following code snippet represents a dictionary of lists.

{
    'fruits': ['apple', 'banana', 'cherry'],
    'colors': ['red', 'blue', 'green'],
    'numbers': [1, 2, 3, 4, 5]
}

Here 'fruits', 'colors', and 'numbers' are the keys of the dictionary and their respective values are the lists.

In this tutorial, you will learn how to create a dictionary of lists, how to access a dictionary of lists, how to update a dictionary of lists, how to traverse through a dictionary of lists, with examples.

1. Creating a dictionary of lists in Python

You can use a dictionary initializer {} to create a dictionary of lists, as shown in the following program.

Python Program

my_dict = {
    'fruits': ['apple', 'banana', 'cherry'],
    'colors': ['red', 'blue', 'green'],
    'numbers': [1, 2, 3, 4, 5]
}

print(my_dict)

Output

{'fruits': ['apple', 'banana', 'cherry'], 'colors': ['red', 'blue', 'green'], 'numbers': [1, 2, 3, 4, 5]}

Or, you can assign the keys in dictionary with the required lists.

Python Program

# Create an emtpy dictionary 
my_dict = dict()

# Add items to dictionary
my_dict['fruits'] = ['apple', 'banana', 'cherry']
my_dict['colors'] = ['red', 'blue', 'green']
my_dict['numbers'] = [1, 2, 3, 4, 5]

print(my_dict)

Output

{'fruits': ['apple', 'banana', 'cherry'], 'colors': ['red', 'blue', 'green'], 'numbers': [1, 2, 3, 4, 5]}

2. Reading the lists from a dictionary of lists in Python

You can read the inner lists in a dictionary of lists using the key as index with the dictionary variable.

In the following program, we have a dictionary of lists in my_dict, with some initial values. We shall access the list object whose key is 'fruits' and print it to the output.

Python Program

my_dict = {
    'fruits': ['apple', 'banana', 'cherry'],
    'colors': ['red', 'blue', 'green'],
    'numbers': [1, 2, 3, 4, 5]
}

print(my_dict['fruits'])

Output

['apple', 'banana', 'cherry']

3. Updating the lists in a dictionary of lists in Python

You can modify the lists inside the dictionary of lists, just like any other list.

In the following program, we have a dictionary of lists in my_dict, with some initial values. We shall access the list object whose key is 'fruits' and add a new item to the list.

Python Program

my_dict = {
    'fruits': ['apple', 'banana', 'cherry'],
    'colors': ['red', 'blue', 'green'],
    'numbers': [1, 2, 3, 4, 5]
}

my_dict['fruits'].append('fig')
my_dict['fruits'].append('mango')

print(my_dict)

Output

{'fruits': ['apple', 'banana', 'cherry', 'fig', 'mango'], 'colors': ['red', 'blue', 'green'], 'numbers': [1, 2, 3, 4, 5]}

4. Traversing through a dictionary of lists in Python

You can traverse through the dictionary of lists using a For loop.

In the following program, we traverse through the dictionary of lists my_dict using a For loop.

Python Program

my_dict = {
    'fruits': ['apple', 'banana', 'cherry'],
    'colors': ['red', 'blue', 'green'],
    'numbers': [1, 2, 3, 4, 5]
}

for key, value in my_dict.items():
    print(f"{key}: {value}")

Output

fruits: ['apple', 'banana', 'cherry']
colors: ['red', 'blue', 'green']
numbers: [1, 2, 3, 4, 5]

Summary

In this tutorial, we learned what a dictionary of lists, how to create them, how to access inner lists in the dictionary using keys, and how to update the lists, how to traverse through a dictionary of lists using For loop, with help of well detailed Python programs.