Python program to convert a matrix into a dictionary.

In this python dictionary program, we will convert a matrix into a dictionary.

Steps to solve the program
  1. Take a matrix as input and create an empty list.
  2. Use for loop with the range function to iterate over the length of the dictionary using len().
  3. Add 1 to the length and assign this as the key and its corresponding index value as its value.
  4. Print the output.
				
					matrix = [[1,2,3],[4,5,6]]
dict1 = {}
for i in range(len(matrix)):
    dict1[i+1] = matrix[i]
    
dict1
				
			

Output :

				
					{1: [1, 2, 3], 2: [4, 5, 6]}
				
			

convert string to the dictionary.

check all values are the same in a dictionary.

Leave a Comment