Python Practice › Dictionary Programs › Program 55
Matrix rows to a dictionary
Write a Python program to convert a matrix into a dictionary keyed by row number.
Input
[[1, 2, 3], [4, 5, 6]]
Output
{1: [1, 2, 3], 2: [4, 5, 6]}Approach
enumerate(matrix, start=1).
Solution
matrix = [[1, 2, 3], [4, 5, 6]]print(dict(enumerate(matrix, start=1)))Output
{1: [1, 2, 3], 2: [4, 5, 6]}Learn the concept
New to this topic? Read the lesson, then come back.