Python - Merge List value Keys to Matrix
Last Updated :
27 Apr, 2023
Sometimes, while working with Python dictionary, we can have a problem in which we need to perform the merger of certain keys in dictionary. In this, we tend to form a matrix of resultant singular key. This kind of problem can have applications in data domains. Let's discuss certain way in which this task can be performed.
Input : test_dict = {'Maths': [1, 2], 'gfg': [4, 5], 'CS': [6]}
Output : {'merge_key': [[4, 5], [6], [1, 2]]}
Input : test_dict = {'Maths': [1], 'gfg': [4], 'CS': [9]}
Output : {'merge_key': [[4], [9], [1]]}
Method 1: Using loop + pop() This task can be performed using brute force manner. In this, we iterate the keys and remove the keys using pop, reinitialize them after merging into a matrix.
Python3
# Python3 code to demonstrate working of
# Merge List value Keys to Matrix
# Using loop + pop()
# initializing dictionary
test_dict = {'gfg' : [4, 5, 6],
'is' : [8, 8, 9],
'CS' : [1, 3, 8],
'Maths' : [1, 2]}
# printing original dictionary
print("The original dictionary : " + str(test_dict))
# initializing list
que_list = ['gfg', 'CS', 'Maths']
# Merge List value Keys to Matrix
# Using loop + pop()
new_data = [test_dict.pop(ele) for ele in que_list]
test_dict["merge_key"] = new_data
# printing result
print("The dictionary after merging : " + str(test_dict))
Output :
The original dictionary : {'is': [8, 8, 9], 'gfg': [4, 5, 6], 'Maths': [1, 2], 'CS': [1, 3, 8]} The dictionary after merging : {'is': [8, 8, 9], 'merge_key': [[4, 5, 6], [1, 3, 8], [1, 2]]}
Time Complexity: O(n*m), where n is the length of the list test_dict, m is the length of the list que_list
Auxiliary Space: O(n*m) additional space of size n and m is created where n is the number of elements in the res list, m is the number of elements in the que list
Method 2: Using for loops
Approach
- Initiate a for loop to access dictionary keys
- If key is present in que_list append value of key to a list x
- If not present assign that key and value to new dictionary res
- After end of for loop assign key "merge_key" with value list x
- Display res
Python3
# Python3 code to demonstrate working of
# Merge List value Keys to Matrix
# initializing dictionary
test_dict = {'gfg' : [4, 5, 6],
'is' : [8, 8, 9],
'CS' : [1, 3, 8],
'Maths' : [1, 2]}
# printing original dictionary
print("The original dictionary : " + str(test_dict))
# initializing list
que_list = ['gfg', 'CS', 'Maths']
# Merge List value Keys to Matrix
x=[]
res=dict()
for i in list(test_dict.keys()):
if i in que_list:
x.append(test_dict[i])
else:
res[i]=test_dict[i]
res["merge_key"]=x
# printing result
print("The dictionary after merging : " + str(res))
OutputThe original dictionary : {'gfg': [4, 5, 6], 'is': [8, 8, 9], 'CS': [1, 3, 8], 'Maths': [1, 2]}
The dictionary after merging : {'is': [8, 8, 9], 'merge_key': [[4, 5, 6], [1, 3, 8], [1, 2]]}
Time Complexity : O(N)
Auxiliary Space : O(N)
Method 3: Using dictionary comprehension
This approach uses a dictionary comprehension to create a new dictionary new_data that only includes the keys from que_list. Then, it converts the values of this new dictionary to a list and adds it to the original dictionary under the key "merge_key". This approach should have the same time complexity as the previous methods, but it may require more auxiliary space due to the creation of the new dictionary.
Python3
# Python3 code to demonstrate working of
# Merge List value Keys to Matrix
# Using dictionary comprehension
# initializing dictionary
test_dict = {'gfg' : [4, 5, 6],
'is' : [8, 8, 9],
'CS' : [1, 3, 8],
'Maths' : [1, 2]}
# printing original dictionary
print("The original dictionary : " + str(test_dict))
# initializing list
que_list = ['gfg', 'CS', 'Maths']
# Merge List value Keys to Matrix
# Using dictionary comprehension
new_data = {key: test_dict[key] for key in que_list}
test_dict["merge_key"] = list(new_data.values())
# printing result
print("The dictionary after merging : " + str(test_dict))
OutputThe original dictionary : {'gfg': [4, 5, 6], 'is': [8, 8, 9], 'CS': [1, 3, 8], 'Maths': [1, 2]}
The dictionary after merging : {'gfg': [4, 5, 6], 'is': [8, 8, 9], 'CS': [1, 3, 8], 'Maths': [1, 2], 'merge_key': [[4, 5, 6], [1, 3, 8], [1, 2]]}
Time complexity: O(n), where n is the number of elements in the dictionary,
Auxiliary space: O(n).
Similar Reads
Python | Merge Python key values to list
Sometimes, while working with Python, we might have a problem in which we need to get the values of dictionary from several dictionaries to be encapsulated into one dictionary. This type of problem can be common in domains in which we work with relational data like in web developments. Let's discuss
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
Python - Merge keys by values
Given a dictionary, merge the keys to map to common values. Examples: Input : test_dict = {1:6, 8:1, 9:3, 10:3, 12:6, 4:9, 2:3} Output : {'1-12': 6, '2-9-10': 3, '4': 9, '8': 1} Explanation : All the similar valued keys merged.Input : test_dict = {1:6, 8:1, 9:3, 4:9, 2:3} Output : {'1': 6, '2-9': 3,
7 min read
Python - Group keys to values list
Sometimes, while working with Python dictionaries, we can have problem in which we need to find all possible values of all keys in a dictionary. This utility is quite common and can occur in many domains including day-day programming and school programming. Lets discuss certain way in which this tas
5 min read
Python Program to Convert Tuple Matrix to Tuple List
Given a Tuple Matrix, flatten to tuple list with each tuple representing each column. Example: Input : test_list = [[(4, 5), (7, 8)], [(10, 13), (18, 17)]] Output : [(4, 7, 10, 18), (5, 8, 13, 17)] Explanation : All column number elements contained together. Input : test_list = [[(4, 5)], [(10, 13)]
8 min read
Python | List value merge in dictionary
Sometimes, while working with dictionaries, we can have a problem in which we have many dictionaries and we are required to merge like keys. This problem seems common, but complex is if the values of keys are list and we need to add elements to list of like keys. Let's discuss way in which this prob
5 min read
Merge Key Value Lists into Dictionary Python
Sometimes, while working with lists, we can come forward with a problem in which we need to perform the merge function in which we have the key list and need to create dictionary mapping keys with corresponding value in other list. Let's discuss certain ways in which this task can be performed. Merg
8 min read
Python - Convert Matrix to Custom Tuple Matrix
Sometimes, while working with Python Matrix, we can have a problem in which we need to perform conversion of a Python Matrix to matrix of tuples which a value attached row-wise custom from external list. This kind of problem can have applications in data domains as Matrix is integral DS that is used
6 min read
Convert Each List Element to Key-Value Pair - Python
We are given a list we need to convert it into the key- value pair. For example we are given a list li = ['apple', 'banana', 'orange'] we need to convert it to key value pair so that the output should be like {1: 'apple', 2: 'banana', 3: 'orange'}. We can achieve this by using multiple methods like
3 min read
Python - Retain K match index values from other list
Sometimes, while working with Python lists, we can have a problem in which we need to retain only the strings with match a particular value from the corresponding list at same index. This can have application in many domains. Lets discuss certain ways in which this task can be performed. Method #1 :
8 min read