Adding values to a Dictionary of List using Python



Python dictionaries are built-in data structures used to store data in key-value pairs. A dictionary of lists refers to a dictionary that contains one or more lists as values for any keys. For example,

# Dictionary of Lists in Python:
dict = { "list1" : [1, 2, 3], "list2" : [4, 5, 6] }

You are given a Python dictionary that contains empty lists, your task is to add values to the lists of dictionaries. Consider the following input output scenario:

Input:
{ 'List1': [], 'List2': [] }

Output:
{ 'List1': [1, 2, 3], 'List2': [4, 5, 6] }

Explanation: The list inside dictionary was empty, we added values to it.

There are several approaches to add values to a dictionary of lists. Each approach has its own benefits. We will discuss each of these approaches with example code. In short, those approaches can be listed as:

Add Values to Dictionary of Lists Using append() Function

The append() method is a built-in method that is used to add a single element to the end of an existing iterable (i.e., list, tuples, dictionary). This method can be used to add values to dictionary of lists. Here the steps:

  • First, initialize an empty dictionary.
  • Add empty lists to the dictionary using the specified keys.
  • Use the append() method to add values to the lists by accessing them through their keys.
  • And, then print the dictionary.

Example

In the following example, we are adding values to a dictionary of lists using the append() function:

# Initialize an empty dictionary
my_dict = {}

# Add lists as value
my_dict['key1'] = []
my_dict['key2'] = []

# Add values using append()
my_dict['key1'].append('value1')
my_dict['key1'].append('value2')
my_dict['key2'].append('value3')

# Print the dictionary
print(my_dict)

The output of the above code will be:

{'key1': ['value1', 'value2'], 'key2': ['value3']}

Add Values to Dictionary of Lists Using += Operator

To add values to a dictionary of lists using the += operator, first access the list by its key. Then, use the += operator to concatenate new values to the end of the accessed list.

Let's look at the below steps to understand this better:

  • Create a dictionary where the values are lists.
  • Access the list using its key from the dictionary.
  • Use the += operator to add (concatenate) new items to the list.
  • And, print the updated dictionary.

Example

The example below shows how to use the += operator to add values into a dictionary of lists:

# Initialize an empty dictionary
my_dict = {}

# Add lists as values
my_dict['key1'] = []
my_dict['key2'] = []

# Add values using the += operator
my_dict['key1'] += ['value4', 'value5']
my_dict['key2'] += ['value6']

# Print the dictionary
print(my_dict)

The output of the above code will be:

{'key1': ['value4', 'value5'], 'key2': ['value6']}

Add Values to Dictionary of Lists Using extend() Function

The extend() is another built-in method that is used to add a single element to the end of an existing iterable (i.e., list, tuples, dictionary). In this method, first we need to access the list using keys of dictionary and then apply extend() function to add values to the list.

Here are the steps to add values to dictionary of lists using extend() method:

  • Create a dictionary where each value is a list.
  • Access the list using its key from the dictionary.
  • Use the extend() method to add multiple items to the list.
  • Print the updated dictionary.

Example

The code below shows how to use extend() function to add values into a dictionary.

# Initialize an empty dictionary
my_dict = {}

# Add lists as values
my_dict['key1'] = []
my_dict['key2'] = []

# Add values using the extend() function
my_dict['key1'].extend(['value7', 'value8'])
my_dict['key2'].extend(['value9'])

# Print the dictionary
print(my_dict)

The output of the above code will be:

{'key1': ['value7', 'value8'], 'key2': ['value9']}

Conclusion

We have discussed four approaches to add values to lists within a dictionary. All of these methods involve accessing the list using keys of dictionary and then use a function or operator to append values to the list. If you are not sure that a key for list exist in dictionary, you can use the setdefault() method to initialize the key.

Updated on: 2025-07-11T18:23:15+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements