Python | Clearing list as dictionary value

Clearing a list is a common problem and solution to it has been discussed many times. But sometimes, we don’t have a native list but list is a value to dictionary key. Clearing it is not as easy as clearing an original list. Let’s discuss certain ways in which this can be done.
Method #1: Using loop + clear() This is the most generic method in which we can perform this particular function. We just run a loop till the last dictionary key and clear the key’s list value as they occur using clear function.
Python3
# Python3 code to demonstrate# clearing list as dict. value# using loop + clear()# initializing dict.test_dict = {"Akash": [1, 4, 3], "Nikhil": [3, 4, 1], "Akshat": [7, 8]}# printing original dictprint("The original dict : " + str(test_dict))# using loop + clear()# clearing list as dict. valuefor key in test_dict: test_dict[key].clear()# print resultprint("The dictionary after clearing value list : " + str(test_dict)) |
The original dict : {'Akash': [1, 4, 3], 'Nikhil': [3, 4, 1], 'Akshat': [7, 8]}
The dictionary after clearing value list : {'Akash': [], 'Nikhil': [], 'Akshat': []}
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #2: Using dictionary comprehension We can reduce the lines of code and merge the above functionality using just the dictionary comprehension and clearing the list using the list re-initialization.
Python3
# Python3 code to demonstrate# clearing list as dict. value# using dictionary comprehension# initializing dict.test_dict = {"Akash": [1, 4, 3], "Nikhil": [3, 4, 1], "Akshat": [7, 8]}# printing original dictprint("The original dict : " + str(test_dict))# using dictionary comprehension# clearing list as dict. valuetest_dict = {key: [] for key in test_dict}# print resultprint("The dictionary after clearing value list : " + str(test_dict)) |
The original dict : {'Akash': [1, 4, 3], 'Nikhil': [3, 4, 1], 'Akshat': [7, 8]}
The dictionary after clearing value list : {'Akash': [], 'Nikhil': [], 'Akshat': []}
Time complexity: O(n), where n is the number of keys in the dictionary.
Auxiliary space: O(n), as a new dictionary is created with the same number of keys as the original dictionary.
Method #3: Using the fromkeys() method
We can use the fromkeys() method to create a new dictionary with the specified keys and all values set to an empty list.
Python3
test_dict = {"Akash": [1, 4, 3], "Nikhil": [3, 4, 1], "Akshat": [7, 8]}# printing original dictprint("The original dict :" + str(test_dict))# Get the keys of the dictionarykeys = test_dict.keys()# Create a new dictionary with the same keys and all values set to an empty listtest_dict = dict.fromkeys(keys, [])# print resultprint("The dictionary after clearing value list :" + str(test_dict))# Output: {'Akash': [], 'Nikhil': [], 'Akshat': []}# This code is contributed by Edula Vinay Kumar Reddy |
The original dict :{'Akash': [1, 4, 3], 'Nikhil': [3, 4, 1], 'Akshat': [7, 8]}
The dictionary after clearing value list :{'Akash': [], 'Nikhil': [], 'Akshat': []}
Time Ccomplexity: O(n)
Auxiliary Space: O(n) for getting keys and creating
Method #4: Use the update() method:
The update() method can be used to update the values of a dictionary in-place. It takes a dictionary, or an iterable of key-value pairs, as an argument and updates the dictionary with the key-value pairs from the argument. Here’s an example of using the update() method to clear a list as a value in a dictionary:
In this example, we use the update() method with a dictionary comprehension to clear the lists in the test_dict dictionary. The dictionary comprehension generates a new iterable of key-value pairs, with the keys being the keys from the test_dict dictionary and the values being an empty list. The update() method updates the test_dict dictionary with these key-value pairs, resulting in all lists being cleared.
Python3
# Python code to demonstrate# clearing list as dict. value# using the update() method # Initialize the dictionarytest_dict = {"Akash" : [1, 4, 3], "Nikhil" : [3, 4, 1], "Akshat" : [7, 8]} # Print the original dictionaryprint("The original dict :", test_dict) # Use the update() method to clear the liststest_dict.update((k, []) for k in test_dict) # Print the updated dictionaryprint("The dictionary after clearing value list :", test_dict)# Output:# The original dict : {'Akash': [1, 4, 3], 'Nikhil': [3, 4, 1], 'Akshat': [7, 8]}# The dictionary after clearing value list : {'Akash': [], 'Nikhil': [], 'Akshat': []}#This code is contributed by Edula Vinay Kumar Reddy |
The original dict : {'Akash': [1, 4, 3], 'Nikhil': [3, 4, 1], 'Akshat': [7, 8]}
The dictionary after clearing value list : {'Akash': [], 'Nikhil': [], 'Akshat': []}
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #5 : Using keys() method
Python3
# Python3 code to demonstrate# clearing list as dict value# initializing dict.test_dict = {"Akash" : [1, 4, 3], "Nikhil" : [3, 4, 1], "Akshat" : [7, 8]}# printing original dictprint("The original dict : " + str(test_dict))res=dict()for key in list(test_dict.keys()): res[key]=[] # print resultprint("The dictionary after clearing value list : " + str(res)) |
The original dict : {'Akash': [1, 4, 3], 'Nikhil': [3, 4, 1], 'Akshat': [7, 8]}
The dictionary after clearing value list : {'Akash': [], 'Nikhil': [], 'Akshat': []}
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #6: Using copy() method
You can create a copy of the original dictionary and clear the list values of the copy. This method does not modify the original dictionary and provides an alternative solution.
The steps are:
- Create a copy of the original dictionary using the copy() method.
- Use a loop to iterate through the keys of the copy dictionary.
- Clear the list values of each key in the copy dictionary using the clear() method.
- Print the resulting copy dictionary.
Python3
# Python3 code to demonstrate# clearing list as dict value using copy()# initializing dict.test_dict = {"Akash" : [1, 4, 3], "Nikhil" : [3, 4, 1], "Akshat" : [7, 8]}# printing original dictprint("The original dict : " + str(test_dict))# creating a copy of the original dictres = test_dict.copy()# clearing the list values of the copy dictfor key in res: res[key].clear()# print resultprint("The dictionary after clearing value list : " + str(res)) |
The original dict : {'Akash': [1, 4, 3], 'Nikhil': [3, 4, 1], 'Akshat': [7, 8]}
The dictionary after clearing value list : {'Akash': [], 'Nikhil': [], 'Akshat': []}
Time complexity: O(n), where n is the number of keys in the dictionary.
Auxiliary space: O(n), where n is the number of keys in the dictionary.



