Python Program to Merge a Matrix By the Elements of First Column
Last Updated :
08 May, 2023
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.
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 the 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:
# Merging similar values using setdefault()
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']]
Time Complexity: O(m*n) where m and n is the number of rows and columns respectively in the list “test_list”. T
Auxiliary Space: O(k) additional space of size k is created
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']]
Method 3: Using defaultdict and for loop
Use defaultdict from the collections module, which automatically initializes any new keys with the default value specified (in this case, an empty list). Then, iterate over the list of lists and append the second element of each list to the list corresponding to the first element (key) in the dictionary. Finally, combine the key and values into a new list of lists.
Approach:
- Initialize a list of lists containing key-value pairs.
- Create an empty dictionary to store the merged results.
- Iterate over the list of lists and for each key-value pair:
a. If the key already exists in the dictionary, append the value to the corresponding list.
b. If the key does not exist in the dictionary, create a new list with the value and add it to the dictionary with the key as the key. - Combine the key and values for each dictionary item into a new list of lists.
- Print the final merged list.
Follow the below steps to implement the above idea:
Python3
from collections import defaultdict
# 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 = defaultdict(list)
for key, val in test_list:
res[key].append(val)
# Getting all values
res = [[key] + val for key, val in res.items()]
# Printing result
print("Merged Matrix : " + str(res))
OutputThe original list is : [[4, 'geeks'], [3, 'Gfg'], [4, 'CS'], [4, 'cs'], [3, 'best']]
Merged Matrix : [[4, 'geeks', 'CS', 'cs'], [3, 'Gfg', 'best']]
Time complexity: O(n), where n is the length of the input list. This is because we iterate over the list of lists only once.
Auxiliary space: O(n), where n is the length of the input list. This is because we create a dictionary with at most n keys and values, and the final merged list also has at most n elements.
Method 4: Using itertools.groupby() function
Python3
from itertools import groupby
# initializing list
test_list = [[4, "geeks"], [3, "Gfg"], [4, "CS"],
[4, "cs"], [3, "best"]]
# sorting the list by the first element of each sublist
test_list.sort(key=lambda x: x[0])
# using groupby() to group the sublists by the first element
grouped = groupby(test_list, key=lambda x: x[0])
# creating a dictionary with the first element of each sublist as the key and
# a list of the second elements as the value
res = {key: [val[1] for val in vals] for key, vals in grouped}
# printing result
print("Merged Matrix : " + str(res))
OutputMerged Matrix : {3: ['Gfg', 'best'], 4: ['geeks', 'CS', 'cs']}
Time complexity: O(n log n), where n is the length of the input list.
Auxiliary space: O(n), where n is the length of the input list, since we need to store the sorted list and the dictionary.
Similar Reads
Python | Merge two list of lists according to first element Given two list of lists of equal length, write a Python program to merge the given two lists, according to the first common element of each sublist. Examples: Input : lst1 = [[1, 'Alice'], [2, 'Bob'], [3, 'Cara']] lst2 = [[1, 'Delhi'], [2, 'Mumbai'], [3, 'Chennai']] Output : [[1, 'Alice', 'Delhi'],
6 min read
Python program to Convert Matrix to List of dictionaries Given a Matrix, convert it to a list of dictionaries by mapping similar index values. Input : test_list = [["Gfg", [1, 2, 3]], ["best", [9, 10, 11]]] Output : [{'Gfg': 1, 'best': 9}, {'Gfg': 2, 'best': 10}, {'Gfg': 3, 'best': 11}] Input : test_list = [["Gfg", [1, 2, 3]]] Output : [{'Gfg': 1}, {'Gfg'
4 min read
Python | Merge first and last elements separately in a list Given a list of lists, where each sublist consists of only two elements, write a Python program to merge the first and last element of each sublist separately and finally, output a list of two sub-lists, one containing all first elements and other containing all last elements. Examples: Input : [['
2 min read
Python Program to Construct n*m Matrix from List We are given a list we need to construct a n*m matrix from that list. For example, a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] we need to construct a 3*4 matrix so that resultant output becomes [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] .Using List ComprehensionThis method uses list comprehension
3 min read
Python Program to Convert Matrix to String Program converts a 2D matrix (list of lists) into a single string, where all the matrix elements are arranged in row-major order. The elements are separated by spaces or any other delimiter, making it easy to represent matrix data as a string.Using List ComprehensionList comprehension provides a con
2 min read