Python – Convert Matrix to dictionary

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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


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]}

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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


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]}



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.