In Python, a dictionary is a mutable and unordered collection of key-value pairs. In some cases, you might need to map a single key to multiple values. In this tutorial, we will learn how to add multiple values to the same key in a dictionary in Python.
Using Lists
Step 1: Create an empty dictionary
First, create an empty dictionary by using the curly braces ({}
). This will be used to store our key-values pairs.
1 |
my_dict = {} |
Step 2: Add values to keys in the dictionary
To add multiple values to a key, use a list as the value. If a key does not exist in the dictionary, initialize it with a list containing the value. If the key already exists, append the value to the list.
1 2 3 4 5 |
def add_value_to_key(some_dict, key, value): if key in some_dict: some_dict[key].append(value) else: some_dict[key] = [value] |
Step 3: Test adding values to keys
Now we can use the add_value_to_key
function to add values to our dictionary.
1 2 3 4 5 6 7 |
add_value_to_key(my_dict, 'A', 1) add_value_to_key(my_dict, 'B', 2) add_value_to_key(my_dict, 'A', 3) add_value_to_key(my_dict, 'C', 4) add_value_to_key(my_dict, 'B', 5) print(my_dict) |
This will output:
{'A': [1, 3], 'B': [2, 5], 'C': [4]}
As you can see, the values are added as lists to the respective keys in the dictionary.
Using defaultdict from collections
Another approach to add multiple values to the same key in a dictionary is by using the defaultdict class from the collections
module. defaultdict
is a subclass of the built-in dict
class which overrides the __missing__(key)
method, providing a default value for a nonexistent key.
Step 1: Import defaultdict
Import the defaultdict
class from the collections
module
1 |
from collections import defaultdict |
Step 2: Create a defaultdict with a list as the default factory
1 |
my_default_dict = defaultdict(list) |
This creates a new dictionary-like object, where the default factory function is list
. The factory function is called when a key is not found in the dictionary.
Step 3: Add values to keys in the defaultdict
To add values to a key, simply append the new value to the list.
1 2 3 4 5 6 7 |
my_default_dict['A'].append(1) my_default_dict['B'].append(2) my_default_dict['A'].append(3) my_default_dict['C'].append(4) my_default_dict['B'].append(5) print(my_default_dict) |
This will produce the same output as before:
defaultdict(<class 'list'="">, {'A': [1, 3], 'B': [2, 5], 'C': [4]})
Here, the values are added as lists to the respective keys in the defaultdict, just like in the first method. Note that the defaultdict
object is displayed with its default factory function.
Full Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
def add_value_to_key(some_dict, key, value): if key in some_dict: some_dict[key].append(value) else: some_dict[key] = [value] my_dict = {} add_value_to_key(my_dict, 'A', 1) add_value_to_key(my_dict, 'B', 2) add_value_to_key(my_dict, 'A', 3) add_value_to_key(my_dict, 'C', 4) add_value_to_key(my_dict, 'B', 5) print(my_dict) from collections import defaultdict my_default_dict = defaultdict(list) my_default_dict['A'].append(1) my_default_dict['B'].append(2) my_default_dict['A'].append(3) my_default_dict['C'].append(4) my_default_dict['B'].append(5) print(my_default_dict) |
This code demonstrates both ways of adding multiple values to the same key in a dictionary in Python.
Conclusion
In this tutorial, we learned two methods of adding multiple values to the same key in a dictionary in Python: by using lists and by using the defaultdict
class from the collections
module. Both methods are easy to implement and suitable for various use cases when you need to store multiple values for a single key in a dictionary.