Open In App

Python | Dictionary initialization with common dictionary

Sometimes, while working with dictionaries, we might have an utility in which we need to initialize a dictionary with records values, so that they can be altered later. This kind of application can occur in cases of memoizations in general or competitive programming. Let’s discuss certain way in which this task can be performed. 

Method 1: Using zip() + repeat() The combination of these functions can be used to perform this particular task. In this, the Dictionary value is attached to the keys repeated using the repeat() by help of zip() 



Step-by-step approach :

Below is the implementation of the above approach:






# Python3 code to demonstrate working of
# Dictionary initialization with common dictionary
# Using zip() + repeat()
from itertools import repeat
 
# initialization Dictionary
test_dict = {'gfg' : 1, 'best' : 3}
 
# Using zip() + repeat()
# Dictionary initialization with common dictionary
res = dict(zip(range(4), repeat(test_dict)))
 
# printing result
print("The dictionary with record values : " + str(res))

Output
The dictionary with record values : {0: {'gfg': 1, 'best': 3}, 1: {'gfg': 1, 'best': 3}, 2: {'gfg': 1, 'best': 3}, 3: {'gfg': 1, 'best': 3}}

Time complexity: O(1)
Auxiliary space: O(1)

Method 2: Using dict.fromkeys()

Step-by-step approach:

Below is the implementation of the above approach:




# Python3 code to demonstrate working of
# Dictionary initialization with common dictionary
# Using dict.fromkeys()
 
# initialization Dictionary
test_dict = {'gfg' : 1, 'best' : 3}
 
# Using dict.fromkeys()
# Dictionary initialization with common dictionary
res = dict.fromkeys(range(4), test_dict)
 
# printing result
print("The dictionary with record values : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy

Output
The dictionary with record values : {0: {'gfg': 1, 'best': 3}, 1: {'gfg': 1, 'best': 3}, 2: {'gfg': 1, 'best': 3}, 3: {'gfg': 1, 'best': 3}}

Time Complexity : O(n)
Auxiliary Space : O(n)

Method #3: Using a dictionary comprehension. 

Initializes a dictionary named ‘test_dict’ with two key-value pairs. It then creates a new dictionary ‘res’ with keys ranging from 0 to 3, and assigns the same dictionary object ‘test_dict’ as the value for each key. Finally, it prints the resulting dictionary ‘res’.




test_dict = {'gfg': 1, 'best': 3}
 
res = {key: test_dict for key in range(4)}
 
print("The dictionary with record values : " + str(res))

Output
The dictionary with record values : {0: {'gfg': 1, 'best': 3}, 1: {'gfg': 1, 'best': 3}, 2: {'gfg': 1, 'best': 3}, 3: {'gfg': 1, 'best': 3}}

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 range provided (which is 4 in this case). 

Method 4: Use a for loop and add key-value pairs to the dictionary

The program initializes an empty dictionary called res. It then enters a loop that iterates four times and on each iteration, it assigns the value of test_dict to the dictionary with key i. Finally, the program prints the resulting dictionary res as a string.

In summary:

  1. Initialize an empty dictionary res.
  2. Loop four times and on each iteration, assign the value of test_dict to the dictionary with key I.
  3. Print the resulting dictionary res as a string.




test_dict = {'gfg': 1, 'best': 3}
res = {}
for i in range(4):
    res[i] = test_dict
print("The dictionary with record values : " + str(res))

Output
The dictionary with record values : {0: {'gfg': 1, 'best': 3}, 1: {'gfg': 1, 'best': 3}, 2: {'gfg': 1, 'best': 3}, 3: {'gfg': 1, 'best': 3}}

Time complexity: O(n), where n is the number of iterations in the for loop. 
Auxiliary space: O(n), where n is the number of key-value pairs in the dictionary. repetitions is increased, the space complexity

Method 5: Using the dict() constructor and a generator expression:

In this method, we pass a generator expression to the dict() constructor. The generator expression creates a sequence of (key, value) pairs where each key is a number from 0 to 3, and each value is the test_dict dictionary. The dict() constructor converts this sequence of pairs into a dictionary. This method is similar to using a dictionary comprehension, but with the added benefit of being able to use a generator expression to conserve memory.




# initialization Dictionary
test_dict = {'gfg': 1, 'best': 3}
 
# Using dict() constructor and a generator expression
# Dictionary initialization with common dictionary
res = dict((i, test_dict) for i in range(4))
 
# printing result
print("The dictionary with record values : " + str(res))

Output
The dictionary with record values : {0: {'gfg': 1, 'best': 3}, 1: {'gfg': 1, 'best': 3}, 2: {'gfg': 1, 'best': 3}, 3: {'gfg': 1, 'best': 3}}

Time complexity: O(n), where n is the number of keys in the resulting dictionary. 
Auxiliary space: O(n), as we need to store the resulting dictionary in memory.

Method 6: Using the copy() method

This method creates an empty dictionary res, and then uses a for loop to create a new key-value pair in res for each integer in the range 0 to 3. The value associated with each key is a copy of the dictionary test_dict using the copy() method. This creates a new dictionary with the same key-value pairs as test_dict, but any changes made to the new dictionary will not affect the original test_dict.

Step-by-step approach:

Below is the implementation of the above approach:




# Python3 code to demonstrate working of
# Dictionary initialization with common dictionary
# Using the copy() method
 
# initialization Dictionary
test_dict = {'gfg' : 1, 'best' : 3}
 
# Using the copy() method
# Dictionary initialization with common dictionary
res = {}
for i in range(4):
    res[i] = test_dict.copy()
 
# printing result
print("The dictionary with record values : " + str(res))

Output
The dictionary with record values : {0: {'gfg': 1, 'best': 3}, 1: {'gfg': 1, 'best': 3}, 2: {'gfg': 1, 'best': 3}, 3: {'gfg': 1, 'best': 3}}

Time complexity: O(n), where n is the number of key-value pairs in the original dictionary test_dict.
Auxiliary space: O(n), where n is the number of key-value pairs in the original dictionary test_dict. 


Article Tags :