Python | Clearing list as dictionary value
Last Updated :
22 Apr, 2023
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
test_dict = { "Akash" : [ 1 , 4 , 3 ],
"Nikhil" : [ 3 , 4 , 1 ],
"Akshat" : [ 7 , 8 ]}
print ( "The original dict : " + str (test_dict))
for key in test_dict:
test_dict[key].clear()
print ( "The dictionary after clearing value list : " + str (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': []}
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
test_dict = { "Akash" : [ 1 , 4 , 3 ],
"Nikhil" : [ 3 , 4 , 1 ],
"Akshat" : [ 7 , 8 ]}
print ( "The original dict : " + str (test_dict))
test_dict = {key: [] for key in test_dict}
print ( "The dictionary after clearing value list : " + str (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': []}
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 ]}
print ( "The original dict :" + str (test_dict))
keys = test_dict.keys()
test_dict = dict .fromkeys(keys, [])
print ( "The dictionary after clearing value list :" + str (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': []}
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
test_dict = { "Akash" : [ 1 , 4 , 3 ],
"Nikhil" : [ 3 , 4 , 1 ],
"Akshat" : [ 7 , 8 ]}
print ( "The original dict :" , test_dict)
test_dict.update((k, []) for k in test_dict)
print ( "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': []}
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #5 : Using keys() method
Python3
test_dict = { "Akash" : [ 1 , 4 , 3 ],
"Nikhil" : [ 3 , 4 , 1 ],
"Akshat" : [ 7 , 8 ]}
print ( "The original dict : " + str (test_dict))
res = dict ()
for key in list (test_dict.keys()):
res[key] = []
print ( "The dictionary after clearing value list : " + str (res))
|
Output
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
test_dict = { "Akash" : [ 1 , 4 , 3 ],
"Nikhil" : [ 3 , 4 , 1 ],
"Akshat" : [ 7 , 8 ]}
print ( "The original dict : " + str (test_dict))
res = test_dict.copy()
for key in res:
res[key].clear()
print ( "The dictionary after clearing value list : " + str (res))
|
Output
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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...