Open In App

Python – Initialize dictionary keys with Matrix

Last Updated : 25 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate working of
# Initialize dictionary keys with Matrix
# Using list comprehension
 
# initializing N
num = 4
 
# Initialize dictionary keys with Matrix
# Using list comprehension
res = {'gfg': [[] for _ in range(num)], 'best': [[] for _ in range(num)]}
 
# printing result
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




# Python3 code to demonstrate working of
# Initialize dictionary keys with Matrix
# Using deepcopy()
from copy import deepcopy
 
# initializing N
num = 4
 
# Initialize dictionary keys with Matrix
# Using deepcopy()
temp = [[] for idx in range(num)]
res = {'gfg': deepcopy(temp), 'best': deepcopy(temp)}
 
# printing result
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', '', ''], ['', '', '', ''], ['', '', '', ''], ['', '', '', '']]
 
# initializing dictionary with matrix keys using dictionary comprehension with zip
d = {(i, j): k for (i, j), k in zip(itertools.product(range(n), range(n)), itertools.chain(*matrix))}
 
# printing the initialized dictionary
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:

  1. Initialize a variable ‘num’ with the value 4.
  2. 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’.
  3. Print the initialized dictionary.
  4. Initialize an empty dictionary named ‘res’.
  5. 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.
  6. Print the initialized dictionary.

Implementation:

Python3




# Python3 code to demonstrate working of
# Initialize dictionary keys with Matrix
# Using for loop
 
# initializing N
num = 4
 
# Initialize dictionary keys with Matrix
# Using for loop
res = {'gfg': [], 'best': []}
for i in range(num):
    res['gfg'].append([])
    res['best'].append([])
 
# printing result
print("The Initialized dictionary : " + str(res))


Output

The Initialized dictionary : {'gfg': [[], [], [], []], 'best': [[], [], [], []]}

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads