using a stack data structure and iterative approach. In Python

We can push the original dictionary on the stack, then we will iterate over each element of the dictionary using a loop, if the element is a dictionary, we push it to the stack, if it is a list, we replace it with the value of K. We will repeat this process until the stack is empty.

Here’s the step-by-step approach:

  • Initialize a stack with the original dictionary.
  • Initialize K value to 4.
  • While the stack is not empty:
    a. Pop the top element from the stack.
    b. If an element is a dictionary, loop over its items and push each value to the stack.
    c. If the element is a list, replace it with the value of K.
  • Return the modified dictionary.

Below is the implementation of the above approach:

Python3




# helper function
def reinitialize_dict(d, K):
    stack = [d]
    while stack:
        ele = stack.pop()
        if isinstance(ele, dict):
            for key, val in ele.items():
                stack.append(val)
        elif isinstance(ele, list):
            for i in range(len(ele)):
                ele[i] = K
    return d
 
# initializing dictionary
test_dict = {'gfg': [4, 6, 7], 'is': 8, 'best': [[4, 5], [8, 9, 20]]}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# initializing K
K = 4
 
# Reinitialize Value lists to K in Dictionary
res = reinitialize_dict(test_dict, K)
 
# printing result
print("The Reinitialized dictionary : " + str(res))


Output

The original dictionary : {'gfg': [4, 6, 7], 'is': 8, 'best': [[4, 5], [8, 9, 20]]}
The Reinitialized dictionary : {'gfg': [4, 4, 4], 'is': 8, 'best': [4, 4]}

Time complexity: O(N), where N is the total number of elements in the dictionary and its nested structures.
Auxiliary space: O(N), where N is the total number of elements in the dictionary and its nested structures.

Python – Reinitialize Value lists to K in Dictionary

Sometimes, while working with Python dictionary values, we can have a problem in which we need to reinitialize all the values lists of all keys in dictionary to a constant K. This kind of problem can have application in domains which use data, like Machine Learning and Data Science. Let’s discuss certain way in which this task can be performed.

Input : test_dict = {‘Gfg’ : [[4, 5], [8, 9, 20], [1, 3, 4, ‘oops’]]} 
Output : {‘Gfg’: [[4, 4], [4, 4, 4], [4, 4, 4, 4]]}

Input : test_dict = {‘Gfg’ : “best”} 
Output : {‘Gfg’ : 4} 

Similar Reads

Method 1: Using recursion + type() + dictionary comprehension + items() + loop

The combination of above functionalities can help to solve this problem. In this, we perform the assignment of values using dictionary comprehension and types are tested using type. The items() is used to extract values from dictionary and to perform to each nesting is handled by recursion....

Method #2: Using recursion and isinstance() function

...

Method 3: using a stack data structure and iterative approach.

Approach...

Method 4 : Using the built-in map() function and lambda function

...

Contact Us