VOOZH about

URL: https://www.geeksforgeeks.org/python/python-clearing-list-as-dictionary-value/

⇱ Python | Clearing list as dictionary value - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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. 


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. 


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.


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.


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


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.

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.

Comment