Open In App

Python – Convert Matrix to dictionary

Last Updated : 14 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a Matrix, convert it into dictionary with keys as row number and value as nested list.

Input : test_list = [[5, 6, 7], [8, 3, 2]] Output : {1: [5, 6, 7], 2: [8, 3, 2]} Explanation : Matrix rows are paired with row number in order. Input : test_list = [[5, 6, 7]] Output : {1: [5, 6, 7]} Explanation : Matrix rows are paired with row number in order.

Method #1 : Using dictionary comprehension + range()

The combination of above functions can be used to solve this problem. In this, we perform the task of iteration using dictionary comprehension and range() can be used to perform numbering of rows. 

Python3




# Python3 code to demonstrate working of
# Convert Matrix to dictionary
# Using dictionary comprehension + range()
 
# initializing list
test_list = [[5, 6, 7], [8, 3, 2], [8, 2, 1]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# using dictionary comprehension for iteration
res = {idx + 1 : test_list[idx] for idx in range(len(test_list))}
 
# printing result
print("The constructed dictionary : " + str(res))


Output

The original list is : [[5, 6, 7], [8, 3, 2], [8, 2, 1]]
The constructed dictionary : {1: [5, 6, 7], 2: [8, 3, 2], 3: [8, 2, 1]}

Time Complexity: O(n) where n is the number of elements in the list test_list.
Auxiliary Space: O(n), where n is the number of elements in the list test_list.

Method #2 : Using dictionary comprehension + enumerate()

The combination of above functions can be used to solve this problem. In this, dictionary comprehension help in construction of dictionary and enumerate() helps in iteration like range() in above method.

Python3




# Python3 code to demonstrate working of
# Convert Matrix to dictionary
# Using dictionary comprehension + enumerate()
 
# initializing list
test_list = [[5, 6, 7], [8, 3, 2], [8, 2, 1]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# enumerate used to perform assigning row number
res = {idx: val for idx, val in enumerate(test_list, start = 1)}
 
# printing result
print("The constructed dictionary : " + str(res))


Output

The original list is : [[5, 6, 7], [8, 3, 2], [8, 2, 1]]
The constructed dictionary : {1: [5, 6, 7], 2: [8, 3, 2], 3: [8, 2, 1]}


Previous Article
Next Article

Similar Reads

Python | Convert flattened dictionary into nested dictionary
Given a flattened dictionary, the task is to convert that dictionary into a nested dictionary where keys are needed to be split at '_' considering where nested dictionary will be started. Method #1: Using Naive Approach Step-by-step approach : Define a function named insert that takes two parameters, a dictionary (dct) and a list (lst). This functi
8 min read
Python | Convert nested dictionary into flattened dictionary
Given a nested dictionary, the task is to convert this dictionary into a flattened dictionary where the key is separated by '_' in case of the nested key to be started. Method #1: Using Naive Approach Step-by-step approach : The function checks if the input dd is a dictionary. If it is, then it iterates over each key-value pair in the dictionary, a
8 min read
Convert String Dictionary to Dictionary Python
Interconversions of data types have been discussed many times and have been quite a popular problem to solve. This article discusses yet another problem of interconversion of the dictionary, in string format to a dictionary. Let's discuss certain ways in which this can be done. Convert String Dictionary to Dictionary Using json.loads() This task ca
6 min read
Convert Dictionary Value list to Dictionary List Python
Sometimes, while working with Python Dictionaries, we can have a problem in which we need to convert dictionary list to nested records dictionary taking each index of dictionary list value and flattening it. This kind of problem can have application in many domains. Let's discuss certain ways in which this task can be performed. Input : test_list =
9 min read
Python - Convert Coordinate Dictionary to Matrix
Sometimes, while working with Python Matrix, we can have problem in which we have dictionary records with key as matrix position and its value, and we wish to convert that to actual Matrix. This can have applications in many domains including competitive programming and day-day programming. Lets discuss certain ways in which this task can be perfor
4 min read
Python - Convert Matrix to Coordinate Dictionary
Sometimes, while working with Python dictionaries, we can have problem in which we need to perform the conversion of matrix elements to their coordinate list. This kind of problem can come in many domains including day-day programming and competitive programming. Lets discuss certain ways in which this task can be performed. Input : test_list = [['
3 min read
Python program to Convert Matrix to Dictionary Value List
Given Matrix, the task is to write a Python program to map each column's values as customized keys from another list. Input : test_list = [[4, 5, 6], [1, 3, 5], [3, 8, 1], [10, 3, 5]], map_list = [4, 5, 6]Output : {4: [4, 1, 3, 10], 5: [5, 3, 8, 3], 6: [6, 5, 1, 5]}Explanation : 4 is mapped with all the 0th index of lists, 4, 1 ,3, 10. Input : test
4 min read
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
7 min read
Python - Update dictionary with other dictionary
Sometimes, while working with Python dictionaries, we can have problem in which we need to perform the update of dictionary with other keys of dictionary. This can have applications in domains in which we need to add certain records to previously captured records. Let's discuss certain ways in which this task can be performed. Method #1 : Using loo
9 min read
Python - Replace dictionary value from other dictionary
Given two dictionaries, update the values from other dictionary if key is present in other dictionary. Input : test_dict = {"Gfg" : 5, "is" : 8, "Best" : 10, "for" : 8, "Geeks" : 9}, updict = {"Geeks" : 10, "Best" : 17} Output : {'Gfg': 5, 'is': 8, 'Best': 17, 'for': 8, 'Geeks': 10} Explanation : "Geeks" and "Best" values updated to 10 and 17. Inpu
6 min read
three90RightbarBannerImg