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 :
- Import the repeat function from the itertools module.
- Initialize a dictionary test_dict with key-value pairs.
- Call the zip() function with two arguments:
a. The range() function to create a sequence of numbers from 0 to 3 (inclusive).
b. The repeat() function with the test_dict dictionary as its argument to create an iterable that returns test_dict infinitely. - Pass the output of zip() to the dict() function to create a dictionary from the key-value pairs generated by zip().
- Assign the resulting dictionary to the variable res.
- Print the resulting dictionary res.
Below is the implementation of the above approach:
Python3
# 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)) |
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:
- Uses the ‘dict.fromkeys()’ method to initialize a new dictionary named ‘res’. The ‘range(4)’ function creates a sequence of numbers from 0 to 3, and the ‘test_dict’ dictionary is passed as the second argument to the ‘dict.fromkeys()’ method. This creates a new dictionary where each key in the range sequence maps to the same value (which is the entire ‘test_dict’ dictionary).
- Finally, prints the resulting dictionary using the ‘print()’ function. The resulting dictionary is stored in the ‘res’ variable, and it contains 4 key-value pairs, where each key is a number from 0 to 3, and each value is the same dictionary as ‘test_dict’.
Below is the implementation of the above approach:
Python3
# 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 |
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’.
Python3
test_dict = { 'gfg' : 1 , 'best' : 3 } res = {key: test_dict for key in range ( 4 )} print ( "The dictionary with record values : " + str (res)) |
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:
- Initialize an empty dictionary res.
- Loop four times and on each iteration, assign the value of test_dict to the dictionary with key I.
- Print the resulting dictionary res as a string.
Python3
test_dict = { 'gfg' : 1 , 'best' : 3 } res = {} for i in range ( 4 ): res[i] = test_dict print ( "The dictionary with record values : " + str (res)) |
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.
- Use the dict() constructor and a generator expression to create a new dictionary called res. The generator expression is (i, test_dict) for i in range(4), which creates a sequence of (key, value) pairs. In this case, the keys are the numbers from 0 to 3, and the values are the entire test_dict dictionary.
- The dict() constructor takes this sequence of (key, value) pairs and converts it into a dictionary. The resulting dictionary has four keys (0, 1, 2, and 3) and each key has the same value, which is the test_dict dictionary.
- Finally, print the resulting dictionary res using the print() function and a formatted string. The str() function is used to convert the dictionary to a string so that it can be concatenated with the rest of the string.
Python3
# 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)) |
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:
- Define a dictionary called test_dict with two key-value pairs.
- Create an empty dictionary called res.
- Use a for loop to iterate over the range of integers from 0 to 3:
a. For each integer i in the range, create a new key-value pair in res where the key is i and the value is a copy of test_dict. - Print the resulting dictionary res as a string.
Below is the implementation of the above approach:
Python3
# 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)) |
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.