Python - Columns to Dictionary Conversion in Matrix
Last Updated :
29 Aug, 2020
Given a Matrix, Convert to Dictionary with elements in 1st row being keys, and subsequent rows acting as values list.
Input : test_list = [[4, 5, 7], [10, 8, 4], [19, 4, 6], [9, 3, 6]]
Output : {4: [10, 19, 9], 5: [8, 4, 3], 7: [4, 6, 6]}
Explanation : All columns mapped with 1st row elements. Eg. 4 -> 10, 19, 9.
Input : test_list = [[4, 5, 7], [10, 8, 4], [19, 4, 6], [9, 3, 7]]
Output : {4: [10, 19, 9], 5: [8, 4, 3], 7: [4, 6, 7]}
Explanation : All columns mapped with 1st row elements. Eg. 7 -> 4, 6, 7.
Method #1 : Using list comprehension + dictionary comprehension
This is one of the ways in which this task can be performed. In this, list comprehension is responsible for construction of values and mapping and dictionary conversion is done using dictionary comprehension.
Python3
# Python3 code to demonstrate working of
# Columns to Dictionary Conversion in Matrix
# Using dictionary comprehension + list comprehension
# initializing list
test_list = [[4, 5, 7], [10, 8, 3], [19, 4, 6], [9, 3, 6]]
# printing original list
print("The original list : " + str(test_list))
# dictionary comprehension performing re making of result
# dictionary
res = {test_list[0][idx]: [test_list[ele][idx]
for ele in range(1, len(test_list))]
for idx in range(len(test_list[0]))}
# printing result
print("Reformed dictionary : " + str(res))
OutputThe original list : [[4, 5, 7], [10, 8, 3], [19, 4, 6], [9, 3, 6]]
Reformed dictionary : {4: [10, 19, 9], 5: [8, 4, 3], 7: [3, 6, 6]}
Method #2 : Using dictionary comprehension + zip()
This is yet another way to solve this problem. In this, we map all column elements with each other using zip() and dictionary comprehension is used to perform remaking of dictionary.
Python3
# Python3 code to demonstrate working of
# Columns to Dictionary Conversion in Matrix
# Using dictionary comprehension + zip()
# initializing list
test_list = [[4, 5, 7], [10, 8, 3], [19, 4, 6], [9, 3, 6]]
# printing original list
print("The original list : " + str(test_list))
# appropriate slicing before zip function
res = {ele[0]: list(ele[1:]) for ele in zip(*test_list)}
# printing result
print("Reformed dictionary : " + str(res))
OutputThe original list : [[4, 5, 7], [10, 8, 3], [19, 4, 6], [9, 3, 6]]
Reformed dictionary : {4: [10, 19, 9], 5: [8, 4, 3], 7: [3, 6, 6]}
Similar Reads
Python - Convert Matrix to Dictionary The task of converting a matrix to a dictionary in Python involves transforming a 2D list or matrix into a dictionary, where each key represents a row number and the corresponding value is the row itself. For example, given a matrix li = [[5, 6, 7], [8, 3, 2], [8, 2, 1]], the goal is to convert it i
4 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 dis
4 min read
Convert Matrix to Dictionary Value List - Python We are given a matrix and the task is to map each column of a matrix to customized keys from a list. For example, given a matrix li = [[4, 5, 6], [1, 3, 5], [3, 8, 1], [10, 3, 5]] and a list map_li = [4, 5, 6], the goal is to map the first column to the key 4, the second column to the key 5, and the
3 min read
Convert a Dictionary to a List in Python In Python, dictionaries and lists are important data structures. Dictionaries hold pairs of keys and values, while lists are groups of elements arranged in a specific order. Sometimes, you might want to change a dictionary into a list, and Python offers various ways to do this. How to Convert a Dict
3 min read
Python - Key Columns Dictionary from Matrix Given a Matrix and list of keys, map each column's values with a custom list key. Input : test_list1 = [[4, 6], [8, 6]], test_list2 = ["Gfg", "Best"]Â Output : {'Gfg': [4, 8], 'Best': [6, 6]}Â Explanation : Column wise, Key values assignment.Input : test_list1 = [[4], [6]], test_list2 = ["Gfg"]Â Out
6 min read