Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python Program to Merge a Matrix By the Elements of First Column

  • Last Updated : 11 Oct, 2020

Given a Matrix, perform merge on the basis of the element in the first column.

Input : test_list = [[4, “geeks”], [3, “Gfg”], [4, “CS”], [4, “cs”], [3, “best”]] 
Output : [[4, ‘geeks’, ‘CS’, ‘cs’], [3, ‘Gfg’, ‘best’]] 
Explanation : 4 is paired with geeks, CS and cs hence are merged into 1 row.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

Input : test_list = [[4, “geeks”], [3, “Gfg”], [4, “CS”], [5, “cs”], [3, “best”]] 
Output : [[4, ‘geeks’, ‘CS’, ‘cs’], [3, ‘Gfg’, ‘best’], [5, ‘cs’]] 
Explanation : 4 is paired with geeks and CS hence are merged into 1 row. 
 



Method 1 : Using setdefault() and list comprehension

In this, the task of grouping is done using setdefault(), which assigns key as first column element and rest of elements as values of list. List comprehension is used post that to get all the values from dictionary constructed.

Python3




# initializing list
test_list = [[4, "geeks"], [3, "Gfg"], [4, "CS"], 
             [4, "cs"], [3, "best"]]
  
# printing original list
print("The original list is : " + str(test_list))
  
res = {}
for key, val in test_list:
  
    # setdefault used to merge similar values
    res.setdefault(key, []).append(val)
  
# getting all values
res = [[key] + val for key, val in res.items()]
  
# printing result
print("Merged Matrix : " + str(res))

Output:

The original list is : [[4, ‘geeks’], [3, ‘Gfg’], [4, ‘CS’], [4, ‘cs’], [3, ‘best’]]
Merged Matrix : [[4, ‘geeks’, ‘CS’, ‘cs’], [3, ‘Gfg’, ‘best’]]

Method 2 : Using values() and setdefault()

Here, we extract the values using values(), rest all the operations are performed in a similar manner as explained above. This program omits from the list the first column element based on which grouping was performed.

Python3




# initializing list
test_list = [[4, "geeks"], [3, "Gfg"], [4, "CS"],
             [4, "cs"], [3, "best"]]
  
# printing original list
print("The original list is : " + str(test_list))
  
res = {}
for key, val in test_list:
  
    # setdefault used to merge similar values
    res.setdefault(key, []).append(val)
  
# fetch values using value()
res = list(res.values())
  
# printing result
print("Merged Matrix : " + str(res))

Output:

The original list is : [[4, ‘geeks’], [3, ‘Gfg’], [4, ‘CS’], [4, ‘cs’], [3, ‘best’]]
Merged Matrix : [[‘geeks’, ‘CS’, ‘cs’], [‘Gfg’, ‘best’]]




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!