Open In App

Python – Group similar elements into Matrix

Last Updated : 15 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with Python Matrix, we can have a problem in which we need to perform grouping of all the elements with are the same. This kind of problem can have applications in data domains. Let’s discuss certain ways in which this task can be performed.

Input : test_list = [1, 3, 4, 4, 2, 3] 
Output : [[1], [2], [3, 3], [4, 4]] 
Input : test_list = [1, 3, 4, 2] 
Output : [[1], [2], [3], [4]]

Method #1: Using list comprehension + groupby()

The combination of the above functions provides a possible solution to this problem. In this, we perform the task of grouping using groupby() and list comprehension assists in iteration.

Python3




# Python3 code to demonstrate working of
# Group similar elements into Matrix
# Using list comprehension + groupby()
from itertools import groupby
 
# initializing list
test_list = [1, 3, 5, 1, 3, 2, 5, 4, 2]
 
# printing original list
print("The original list : " + str(test_list))
 
# Group similar elements into Matrix
# Using list comprehension + groupby()
res = [list(val) for key, val in groupby(sorted(test_list))]
 
# printing result
print("Matrix after grouping : " + str(res))


Output : 

The original list : [1, 3, 5, 1, 3, 2, 5, 4, 2]
Matrix after grouping : [[1, 1], [2, 2], [3, 3], [4], [5, 5]]

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. This is because the result list ‘res’ will have at most n elements, where each element is a list containing the grouped similar elements.

Method #2: Using list comprehension + Counter()

This is yet another approach to this problem. In this, we get the values along with its frequency using Counter() and then employ list comprehension to multiply each element with frequency to get duplicates 

Approach:

  1. Import the Counter() function from the collections module.
  2. Initialize the input list.
  3. Use the Counter() function to count the frequency of each element in the list.
  4. Use a list comprehension to create a list of sublists, where each sublist contains the key element repeated by the value count number of times.
  5. Print the resulting list of sublists.

Below is the implementation of the above approach:

Python3




# Python3 code to demonstrate working of
# Group similar elements into Matrix
# Using list comprehension + Counter()
 
from collections import Counter
 
# Initializing list
test_list = [1, 3, 5, 1, 3, 2, 5, 4, 2]
 
# Printing original list
print("The original list : " + str(test_list))
 
# Grouping similar elements into Matrix
# Using list comprehension + Counter()
temp = Counter(test_list)
res = [[key] * val for key, val in temp.items()]
 
# Printing the result
print("Matrix after grouping : " + str(res))


Output : 

The original list : [1, 3, 5, 1, 3, 2, 5, 4, 2]
Matrix after grouping : [[1, 1], [2, 2], [3, 3], [4], [5, 5]]

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.

Method #3: Using defaultdict

defaultdict is a subclass of the built-in dict class and provides a default value for a nonexistent key. Use a defaultdict to group similar elements into a list.

Python3




from collections import defaultdict
 
# Initializing list
test_list = [1, 3, 5, 1, 3, 2, 5, 4, 2]
 
# Printing original list
print("The original list : " + str(test_list))
 
# Grouping similar elements into Matrix
# Using defaultdict
d = defaultdict(list)
 
for element in test_list:
    d[element].append(element)
res = list(d.values())
 
# Printing the result
print("Matrix after grouping : " + str(res))


Output

The original list : [1, 3, 5, 1, 3, 2, 5, 4, 2]
Matrix after grouping : [[1, 1], [3, 3], [5, 5], [2, 2], [4]]

Time Complexity: O(n), where n is the length of the input list, 
Auxiliary Space: O(n), as we are using a defaultdict to store the groups of similar elements.

Method #4: Using nested loops to iterate through the list and build the matrix.

  1. This code first initializes an empty matrix res. It then iterates through the original list test_list and for each element i.
  2. It checks if there is already a sublist in ‘res’ that contains ‘i’. 
  3. If there is, it appends ‘i’ to that sublist. If not, it creates a new sublist containing only i and appends it to res. 
  4. In the end, res contains the desired matrix after grouping.

Python3




# input list
test_list = [1, 3, 5, 1, 3, 2, 5, 4, 2]
res = []
 
for i in test_list:
 
    found = False
 
    for j in range(len(res)):
 
        # Element found
        if i in res[j]:
            res[j].append(i)
            found = True
            break
    # Not found
    if not found:
        res.append([i])
 
        # Printing the result
print("Matrix after grouping : " + str(res))


Output

Matrix after grouping : [[1, 1], [3, 3], [5, 5], [2, 2], [4]]

Time complexity: O(n^2), where n is the length of test_list.
Auxiliary space: O(n), where n is the length of test_list. 

Method 5: Using dictionary to group the elements of the list.

Approach:

  1. Initialize an empty dictionary group_dict.
  2. Iterate through the elements of the input list test_list.
  3. For each element, check if it is already a key in the group_dict.
  4. If it is not a key, add it as a key with a value of a list containing the element.
  5. If it is already a key, append the element to the corresponding list.
  6. Convert the dictionary to a list of lists to get the desired matrix form.
  7. Print the resulting matrix.

Below is the implementation of the above approach:

Python3




# Input list
test_list = [1, 3, 5, 1, 3, 2, 5, 4, 2]
group_dict = {}
 
# Step 2-5
for element in test_list:
    if element not in group_dict:
        group_dict[element] = [element]
    else:
        group_dict[element].append(element)
 
# Step 6
res = list(group_dict.values())
 
# Step 7
print("Matrix after grouping: " + str(res))


Output

Matrix after grouping: [[1, 1], [3, 3], [5, 5], [2, 2], [4]]

Time complexity: O(n)
Auxiliary space: O(n) for the dictionary.

Method 6: Use the set() function 

Here we will get unique elements of the list, and then use list comprehension to create a new list of lists with all occurrences of each unique element.

Approach:

  1. Initialize an empty list to store the grouped elements.
  2. Use the set() function to get unique elements of the input list.
  3. Loop through each unique element.
  4. Use list comprehension to create a new list of lists with all occurrences of each unique element.
  5. Append the new list to the empty list initialized in step 1.
  6. Return the list of lists.

Below is the implementation of the above approach:

Python3




test_list = [1, 3, 5, 1, 3, 2, 5, 4, 2]
 
# Step 1
grouped = []
 
# Step 2
unique = set(test_list)
 
# Step 3
for element in unique:
     
    # Step 4
    sublist = [x for x in test_list if x == element]
     
    # Step 5
    grouped.append(sublist)
 
# Step 6
print("Matrix after grouping: " + str(grouped))


Output

Matrix after grouping: [[1, 1], [2, 2], [3, 3], [4], [5, 5]]

Time complexity: O(n^2), where n is the length of the input list. 
Auxiliary space: O(n), where n is the length of the input list.

Method 6: Use the set() without using list comprehension

Python3




test_list = [1, 3, 5, 1, 3, 2, 5, 4, 2]
 
# Step 1: Use set() to get unique elements of the list
unique = set(test_list)
 
# Step 2: Create a list of lists for the grouped elements
grouped = [[] for _ in range(len(unique))]
 
# Step 3: Iterate through the test_list and append each element
# to the corresponding sublist in grouped
for i, element in enumerate(unique):
    for x in test_list:
        if x == element:
            grouped[i].append(x)
 
# Step 4: Print the matrix
print("Matrix after grouping: " + str(grouped))


Output

Matrix after grouping: [[1, 1], [2, 2], [3, 3], [4], [5, 5]]

Time complexity: O(n^2), where n is the length of the input list.
Auxiliary space: O(n), where n is the length of the input list. 

Method 7: Using NumPy

  • Import the NumPy library
  • Use np.unique() to get unique elements of the list
  • Use np.zeros() to create a matrix of zeros with the shape (len(unique), max(counts))
  • Use nested loops to iterate through the unique elements and their counts, and fill in the matrix with the Step 5: Print the matrix

Python3




import numpy as np
 
test_list = [1, 3, 5, 1, 3, 2, 5, 4, 2]
 
# Step 1: Use np.unique() to get unique elements of the list
unique, counts = np.unique(test_list, return_counts=True)
 
# Step 2: Use np.zeros() to create a matrix of zeros with the shape (len(unique), max(counts))
matrix = np.zeros((len(unique), np.max(counts)), dtype=int)
 
# Step 3: Use nested loops to iterate through the unique elements and their counts, and fill in the matrix with the corresponding elements
for i, element in enumerate(unique):
    for j in range(counts[i]):
        matrix[i,j] = element
 
# Step 4: Remove the extra zeros
result = []
for row in matrix:
    result.append([x for x in row if x != 0])
 
# Step 5: Print the matrix
print("Matrix after grouping: " + str(result))


Output:

Matrix after grouping: [[1, 1], [2, 2], [3, 3], [4], [5, 5]]

Time complexity: O(n log n) (worst case for np.unique() to sort the array)
Auxiliary space: O(n * k) (for the matrix, where k is the maximum count of an element)



Previous Article
Next Article

Similar Reads

Python - Group Records on Similar index elements
Sometimes, while working with Python records, we can have a problem in which we need to perform grouping of particular index of tuple, on basis of similarity of rest of indices. This type of problems can have possible applications in Web development domain. Let's discuss certain ways in which this task can be performed. Input : test_list = [(4, 5,
6 min read
Python | Group and count similar records
Sometimes, while working with records, we can have a problem in which we need to collect and maintain the counter value inside records. This kind of application is important in the web development domain. Let's discuss certain ways in which this task can be performed. Method #1 : Using loop + Counter() + set() The combination of the above functiona
6 min read
Python - Group similar value list to dictionary
Sometimes, while working with Python list, we can have a problem in which we need to group similar value lists indices to values into a dictionary. This can have a good application in domains in which we need grouped dictionary as output for pair of lists. Lets discuss certain ways in which this task can be performed. Method #1: Using dictionary co
6 min read
Python - Group Similar keys in dictionary
Sometimes while working with dictionary data, we can have problems in which we need to perform grouping based on the substring of keys and reform the data grouped on similar keys. This can have application in data preprocessing. Let us discuss certain ways in which this task can be performed. Method #1: Using loop This is brute way in which we perf
5 min read
Python - Group Similar Start and End character words
Sometimes, while working with Python data, we can have problem in which we need to group all the words on basis of front and end characters. This kind of application is common in domains in which we work with data like web development. Lets discuss certain ways in which this task can be performed. Method #1 : Using defaultdict() + loop The combinat
5 min read
Python - Group Similar items to Dictionary Values List
Given a list of elements, perform grouping of similar elements, as different key-value lists in a dictionary. Input : test_list = [4, 6, 6, 4, 2, 2, 4, 8, 5, 8] Output : {4: [4, 4, 4], 6: [6, 6], 2: [2, 2], 8: [8, 8], 5: [5]} Explanation : Similar items grouped together on occurrences. Input : test_list = [7, 7, 7, 7] Output : {7 : [7, 7, 7, 7]} Ex
7 min read
Python program to group keys with similar values in a dictionary
Given a dictionary with values as a list. Group all the keys together with similar values. Input : test_dict = {"Gfg": [5, 6], "is": [8, 6, 9], "best": [10, 9], "for": [5, 2], "geeks": [19]} Output : [['Gfg', 'is', 'for'], ['is', 'Gfg', 'best'], ['best', 'is'], ['for', 'Gfg']] Explanation : Gfg has 6, is has 6, and for has 5 which is also in Gfg he
2 min read
Python - Similar index elements Matrix
Sometimes, while working with data, we can have a problem in which we need to perform the construction of matrix from lists element vertically than horizontally. This kind of application can come in Data Science domains in which we need to construct Matrix from several lists. Lets discuss certain ways in which this task can be performed. Method #1:
7 min read
Python - Check Similar elements in Matrix rows
Given a Matrix and list, the task is to write a Python program to check if all the matrix elements of the row are similar to the ith index of the List. Input : test_list = [[1, 1, 1], [4, 4], [3, 3, 3], [5, 5, 5, 5]] Output : True Explanation : All rows have same elements.Input : test_list = [[1, 1, 4], [4, 4], [3, 3, 3], [5, 5, 5, 5]] Output : Fal
8 min read
Python - Convert Lists into Similar key value lists
Given two lists, one of key and other values, convert it to dictionary with list values, if keys map to different values on basis of index, add in its value list. Input : test_list1 = [5, 6, 6, 6], test_list2 = [8, 3, 2, 9] Output : {5: [8], 6: [3, 2, 9]} Explanation : Elements with index 6 in corresponding list, are mapped to 6. Input : test_list1
12 min read
three90RightbarBannerImg