Python – Initialize dictionary keys with Matrix
Last Updated :
25 Apr, 2023
Sometimes, while working with Python Data, we can have a problem in which we need to construct an empty mesh of dictionaries for further population of data. This problem can have applications in many domains which include data manipulation. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using list comprehension This is one of the way in which this task can be performed. In this, we initialize the dictionary keys with empty mesh with N by iterating using list comprehension.
Python3
num = 4
res = { 'gfg' : [[] for _ in range (num)], 'best' : [[] for _ in range (num)]}
print ("The Initialized dictionary : " + str (res))
|
Output :
The Initialized dictionary : {'gfg': [[], [], [], []], 'best': [[], [], [], []]}
Time Complexity: O(n), where n is the length of the list test_list
Auxiliary Space: O(1), constant extra space is required
Method #2 : Using deepcopy() This task can also be performed using deepcopy(). In this, we perform the task of performing copy of each dictionary key as non referenced key.
Python3
from copy import deepcopy
num = 4
temp = [[] for idx in range (num)]
res = { 'gfg' : deepcopy(temp), 'best' : deepcopy(temp)}
print ("The Initialized dictionary : " + str (res))
|
Output :
The Initialized dictionary : {'gfg': [[], [], [], []], 'best': [[], [], [], []]}
Using a dictionary comprehension with zip to initialize the dictionary keys with matrix:
Approach:
nitialize the size of the matrix and the contents of the matrix.
Use itertools.product to create a list of all the possible (i, j) keys in the dictionary.
Use itertools.chain to flatten the matrix into a single list of values.
Use zip to combine the (i, j) keys with the values from the flattened matrix.
Use a dictionary comprehension to create the dictionary with the combined keys and values.
Print the initialized dictionary.
Python3
import itertools
n = 4
matrix = [[ 'gfg' , 'best' , ' ', ' '], [' ', ' ', ' ', ' '], [' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ']]
d = {(i, j): k for (i, j), k in zip (itertools.product( range (n), range (n)), itertools.chain( * matrix))}
print ( "The initialized dictionary:" , d)
|
Output
The initialized dictionary: {(0, 0): 'gfg', (0, 1): 'best', (0, 2): '', (0, 3): '', (1, 0): '', (1, 1): '', (1, 2): '', (1, 3): '', (2, 0): '', (2, 1): '', (2, 2): '', (2, 3): '', (3, 0): '', (3, 1): '', (3, 2): '', (3, 3): ''}
Time Complexity: O(n^2)
Auxiliary Space: O(n^2)
Method 4: Using a for loop to initialize the dictionary keys with matrix
Step-by-Step Approach:
- Initialize a variable ‘num’ with the value 4.
- Initialize a dictionary named ‘res’ with two keys ‘gfg’ and ‘best’. The values of the keys are initialized with empty lists using a list comprehension that iterates over the range of ‘num’.
- Print the initialized dictionary.
- Initialize an empty dictionary named ‘res’.
- Iterate a loop that iterates over the range of ‘num’. Inside the loop, it appends an empty list to the values of each key ‘gfg’ and ‘best’ of the ‘res’ dictionary.
- Print the initialized dictionary.
Implementation:
Python3
num = 4
res = { 'gfg' : [], 'best' : []}
for i in range (num):
res[ 'gfg' ].append([])
res[ 'best' ].append([])
print ( "The Initialized dictionary : " + str (res))
|
Output
The Initialized dictionary : {'gfg': [[], [], [], []], 'best': [[], [], [], []]}
Time Complexity: O(num)
Auxiliary Space: O(num)
Share your thoughts in the comments
Please Login to comment...