In this python dictionary program, we will convert a matrix into a dictionary.
Steps to solve the program
- Take a matrix as input and create an empty list.
- Use for loop with the range function to iterate over the length of the dictionary using len().
- Add 1 to the length and assign this as the key and its corresponding index value as its value.
- 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]}