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
test_dict = { 'gfg' : [ 4 , 5 , 6 ],
'is' : [ 8 , 8 , 9 ],
'CS' : [ 1 , 3 , 8 ],
'Maths' : [ 1 , 2 ]}
print ("The original dictionary : " + str (test_dict))
que_list = [ 'gfg' , 'CS' , 'Maths' ]
new_data = [test_dict.pop(ele) for ele in que_list]
test_dict["merge_key"] = new_data
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
test_dict = { 'gfg' : [ 4 , 5 , 6 ],
'is' : [ 8 , 8 , 9 ],
'CS' : [ 1 , 3 , 8 ],
'Maths' : [ 1 , 2 ]}
print ( "The original dictionary : " + str (test_dict))
que_list = [ 'gfg' , 'CS' , 'Maths' ]
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
print ( "The dictionary after merging : " + str (res))
|
Output
The 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
test_dict = { 'gfg' : [ 4 , 5 , 6 ],
'is' : [ 8 , 8 , 9 ],
'CS' : [ 1 , 3 , 8 ],
'Maths' : [ 1 , 2 ]}
print ( "The original dictionary : " + str (test_dict))
que_list = [ 'gfg' , 'CS' , 'Maths' ]
new_data = {key: test_dict[key] for key in que_list}
test_dict[ "merge_key" ] = list (new_data.values())
print ( "The dictionary after merging : " + str (test_dict))
|
Output
The 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).
Share your thoughts in the comments
Please Login to comment...