Convert Matrix to Dictionary Value List - Python
Last Updated :
28 Jan, 2025
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 third column to the key 6. Hence output will be : {4: [4, 1, 3, 10], 5: [5, 3, 8, 3], 6: [6, 5, 1, 5]}
Using dictionary comprehension
This method maps each column of the matrix to customized keys from another list. zip() function combines the elements of the matrix with the map list and dictionary comprehension is used to create the mappings.
Python
from collections import defaultdict
li = [[4, 5, 6], [1, 3, 5], [3, 8, 1], [10, 3, 5]]
map_li = [4, 5, 6]
# mapping column using zip(), dictionary comprehension for key
temp = [{key : val for key, val in zip(map_li, idx)} for idx in li]
# convert to dictionary value list
res = defaultdict(list)
{res[key].append(sub[key]) for sub in temp for key in sub}
print(str(dict(res)))
Output{4: [4, 1, 3, 10], 5: [5, 3, 8, 3], 6: [6, 5, 1, 5]}
Explanation:
- zip(map_li, idx) pairs each element of map_li with the respective column from li and then dictionary comprehension creates a dictionary for each row.
- defaultdict(list) ensures that for each key there is a list of corresponding column values.
Using Loops
This method iterates through each row and column of the matrix using for loops thereby mapping the elements to their corresponding keys from the map_li list.
Python
li = [[4, 5, 6], [1, 3, 5], [3, 8, 1], [10, 3, 5]]
map_list = [4, 5, 6]
res = {}
for row in li:
for i in range(len(row)):
if map_list[i] not in res:
res[map_list[i]] = []
res[map_list[i]].append(row[i])
print(str(res))
Output{4: [4, 1, 3, 10], 5: [5, 3, 8, 3], 6: [6, 5, 1, 5]}
Using Pandas
Pandas allows us to map columns directly to customized keys from a list and hence this method can be more readable and faster for large datasets.
Python
import pandas as pd
li = [[4, 5, 6], [1, 3, 5], [3, 8, 1], [10, 3, 5]]
map_li = [4, 5, 6]
df = pd.DataFrame(li, columns=map_li)
res = df.to_dict(orient='list')
print(res)
Output{4: [4, 1, 3, 10], 5: [5, 3, 8, 3], 6: [6, 5, 1, 5]}
Explanation:
- matrix li is converted into a Pandas DataFrame with columns labeled by map_li.
- to_dict() is used to convert the DataFrame columns into a dictionary where keys are from map_li and the values are lists of the respective columns from the matrix.
Similar Reads
Python Convert Dictionary to List of Values Python has different types of built-in data structures to manage your data. A list is a collection of ordered items, whereas a dictionary is a key-value pair data. Both of them are unique in their own way. In this article, the dictionary is converted into a list of values in Python using various con
3 min read
Python - Convert dictionary items to values Sometimes, while working with Python dictionary, we can have a problem in which we need to convert all the items of dictionary to a separate value dictionary. This problem can occur in applications in which we receive dictionary in which both keys and values need to be mapped as separate values. Let
3 min read
Convert Dictionary to List of Tuples - Python Converting a dictionary into a list of tuples involves transforming each key-value pair into a tuple, where the key is the first element and the corresponding value is the second. For example, given a dictionary d = {'a': 1, 'b': 2, 'c': 3}, the expected output after conversion is [('a', 1), ('b', 2
3 min read
Python - Convert Key-Value list Dictionary to List of Lists We are given a key value list dictionary we need to convert it list of lists. For example we are given a dictionary a = {'name': 'Geeks', 'age': 8, 'city': 'Noida'} we need to convert this into list of lists so the output should be [['name', 'Geeks'], ['age', 25], ['city', 'Geeks']]. Using List Comp
2 min read
Python - Convert key-values list to flat dictionary We are given a list that contains tuples with the pairs of key and values we need to convert that list into a flat dictionary. For example a = [("name", "Ak"), ("age", 25), ("city", "NYC")] is a list we need to convert it to dictionary so that output should be a flat dictionary {'name': 'Ak', 'age':
3 min read