Open In App

Python | Binary element list grouping

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

Sometimes while working with the databases, we need to perform certain list operations that are more like query language, for instance, grouping of nested list element with respect to its other index elements. This article deals with binary nested list and group each nested list element with respect to its other index elements 

Method #1: Using list comprehension: List comprehension can perform the task that would usually take 4-5 lines in 1-2 lines. This groups the element by assigning to each number values according to the match with another element of list. 

Python3




# Python3 code to demonstrate
# to perform binary list grouping
# using list comprehension
 
# initializing list
test_list = [["G", 0], ["F", 0], ["B", 2],
             ["E", 2], ['I', 1], ['S', 1],
             ['S', 2], ['T', 2], ['G', 0]]
 
# using list comprehension
# to perform binary list grouping
temp = set(map(lambda i: i[1], test_list))
res = [[j[0] for j in test_list if j[1] == i] for i in temp]
 
# printing result
print("The grouped list is : " + str(res))


Output:

The grouped list is : [['G', 'F', 'G'], ['I', 'S'], ['B', 'E', 'S', 'T']]

Time complexity: O(N^2), where N is the number of elements in the input list
Auxiliary space: O(N), where N is the number of elements in the input list.

Method #2: Using itertools.groupby() + itemgetter(): 

We can also use groupby function to perform this particular task. This method follows 2-3 steps. First, the sequence is sort with respect to second element, now this can be fed to get grouped and is then grouped. Then lastly, we print the first element as required by the result. 

Python3




# Python3 code to demonstrate
# to perform binary list grouping
# using itertools.groupby() + itemgetter()
from itertools import groupby
from operator import itemgetter
 
# Initializing list
test_list = [["G", 0], ["F", 0], ["B", 2],
             ["E", 2], ['I', 1], ['S', 1],
             ['S', 2], ['T', 2], ['G', 0]]
 
# Performing binary list grouping
# using itertools.groupby() + itemgetter()
test_list.sort(key=itemgetter(1))
groups = groupby(test_list, itemgetter(1))
res = [[i[0] for i in val] for (key, val) in groups]
 
# Printing the resultant list
print("The grouped list is : " + str(res))


Output:

The grouped list is : [['G', 'F', 'G'], ['I', 'S'], ['B', 'E', 'S', 'T']]

Time complexity: O(nlogn), where ‘n’ is the length of the input list.
Auxiliary space: O(n), as the list ‘res’ can potentially have ‘n’ elements in the worst case, where ‘n’ is the length of the input list.

Method #3: Using collections.defaultdict() 

This is a conventional method of performing hashing of all the keys with the corresponding values in sequence and then printing the values corresponding to the hashed list. 

Python3




# Python3 code to demonstrate
# to perform binary list grouping
# using collections.defaultdict()
import collections
 
# initializing list
test_list = [["G", 0], ["F", 0], ["B", 2],
             ["E", 2], ['I', 1], ['S', 1],
             ['S', 2], ['T', 2], ['G', 0]]
 
# using collections.defaultdict()
# to perform binary list grouping
res = collections.defaultdict(list)
for val in test_list:
    res[val[1]].append(val[0])
 
# printing result
print("The grouped list is : " + str(res.values()))


Output:

The grouped list is : dict_values([['G', 'F', 'G'], ['I', 'S'], ['B', 'E', 'S', 'T']])

Time complexity: O(n), where n is the length of the input list test_list.
Auxiliary space: O(k), where k is the number of unique second values in the input list test_list. 

Method #4: Using for loops+sort()

  1. Initiated a for loop to find unique 1 index elements
  2. Initiated a nested for loop to group all the characters with same 1 index elements
  3. Display the grouped list 

Python3




# Python3 code to demonstrate
# to perform binary list grouping
 
# initializing list
test_list = [["G", 0], ["F", 0], ["B", 2],
             ["E", 2], ['I', 1], ['S', 1],
             ['S', 2], ['T', 2], ['G', 0]]
 
# using list comprehension
# to perform binary list grouping
x = []
for i in test_list:
    if i[1] not in x:
        x.append(i[1])
x.sort()
res = []
for i in x:
    a = []
    for j in test_list:
        if j[1] == i:
            a.append(j[0])
    res.append(a)
# printing result
print("The grouped list is : " + str(res))


Output

The grouped list is : [['G', 'F', 'G'], ['I', 'S'], ['B', 'E', 'S', 'T']]

Time Complexity : O(n*n)
Auxiliary Space : O(n)

Method #5: Using numpy module

  1. Import the numpy library with the alias np.
  2. Create the input list test_list.
  3. Convert test_list to a numpy array using the np.array function and assign the result to the variable arr.
  4. Use the np.unique function to get the unique values of the second column of arr and assign the result to the variable unique_values.
  5. Create an empty list called result_list.
  6. For each unique value i in unique_values, do the following:
         a. Use boolean indexing to select the rows of arr where the second column is equal to i.
         b. Extract the values from the first column of the selected rows and convert them to a Python list.
         c. Append the resulting list to result_list.
  7. Print the resulting grouped list.

Python3




import numpy as np
 
# initializing list
test_list = [["G", 0], ["F", 0], ["B", 2], ["E", 2],
             ['I', 1], ['S', 1], ['S', 2], ['T', 2], ['G', 0]]
 
arr = np.array(test_list)
 
# Storing all unique values
unique_values = np.unique(arr[:, 1])
 
result_list = [list(arr[arr[:, 1] == i, 0]) for i in unique_values]
 
# Printing the result
print("The grouped list is:", result_list)
 
# This code is contributed by Jyothi pinjala.


Output:

The grouped list is: [['G', 'F', 'G'], ['I', 'S'], ['B', 'E', 'S', 'T']]

Time complexity: O(N log N) in the worst case because it has to search for unique values in the second column of the array. 
Auxiliary space: O(N) in the worst case because it has to store a numpy array with the same size as the input list.

Method #6: Using dictionary and setdefault()

This method uses a dictionary to group the items by their second element, which acts as the key in the dictionary. The setdefault() method is used to create an empty list as the value for a new key or append the current item’s first element to the list associated with the key. The resulting dictionary values are then converted to a list to get the final result.

step by step approach :

  1. Initialize a list named test_list containing sub-lists, where each sub-list contains a letter and a number.
  2. Initialize an empty dictionary named group_dict.
  3. Use a for loop to iterate over each item in test_list.
  4. For each item, use the setdefault() method of the dictionary to add a new key-value pair if the key (i.e., the number) does not already exist in the dictionary, and the default value is an empty list. If the key already exists, the method returns the corresponding value (i.e., the list of letters) associated with the key.
  5. Append the letter from the current item to the list of letters associated with the corresponding number key in group_dict.
  6. Convert the dictionary values into a list using the list() function and assign the result to res.
  7. Print the result.

Python3




# Python3 code to demonstrate
# to perform binary list grouping
 
# initializing list
test_list = [["G", 0], ["F", 0], ["B", 2],
            ["E", 2], ['I', 1], ['S', 1],
            ['S', 2], ['T', 2], ['G', 0]]
 
# Using dictionary and setdefault() to perform binary list grouping
group_dict = {}
for item in test_list:
    group_dict.setdefault(item[1], []).append(item[0])
res = list(group_dict.values())
 
# printing result
print ("The grouped list is : " + str(res))


Output

The grouped list is : [['G', 'F', 'G'], ['B', 'E', 'S', 'T'], ['I', 'S']]

Time complexity: O(n)
Auxiliary space: O(n)

Method #7: Using pandas module:

The program performs binary list grouping using the pandas module. It initializes a list called test_list, which contains sublists of two elements each. It then converts the list to a pandas DataFrame, groups the DataFrame by the second element of each sublist, applies a lambda function that selects the first element of each sublist, converts the resulting pandas Series to a list, and prints the grouped list.

Step-by-step approach:

  • Import pandas module.
  • Convert the list to a pandas DataFrame.
  • Group the DataFrame by the second element of each sublist.
  • Apply the lambda function that selects the first element of each sublist.
  • Convert the resulting pandas Series to a list.
  • Print the grouped list.

Below is the implementation of the above approach:

Python3




# Python3 code to demonstrate
# to perform binary list grouping
 
# importing pandas module
import pandas as pd
 
# initializing list
test_list = [["G", 0], ["F", 0], ["B", 2],
            ["E", 2], ['I', 1], ['S', 1],
            ['S', 2], ['T', 2], ['G', 0]]
 
# converting list to DataFrame
df = pd.DataFrame(test_list, columns=['value', 'group'])
 
# grouping by group
grouped = df.groupby('group')['value'].apply(lambda x: x.tolist()).tolist()
 
# printing result
print("The grouped list is: " + str(grouped))


OUTPUT: 

The grouped list is: [['G', 'F', 'G'], ['I', 'S'], ['B', 'E', 'S', 'T']]

Time complexity: O(nlogn) because the DataFrame is sorted by the second element of each sublist.
Auxiliary space: O(n) because a pandas DataFrame is created.

Method  8: Using a list of lists and index

  1. Create a list of empty lists to store the groups.
  2. Iterate over each element in the list.
  3. Use the group number as an index to access the corresponding list in the list of lists.
  4. Append the element to the list for that group.
  5. Convert the list of lists into a list of values to get the final result.

Python3




# Initializing list
test_list = [["G", 0], ["F", 0], ["B", 2],
            ["E", 2], ['I', 1], ['S', 1],
            ['S', 2], ['T', 2], ['G', 0]]
 
num_groups = max([elem[1] for elem in test_list]) + 1
grouped = [[] for _ in range(num_groups)]
 
# Iterating over our input list
for elem in test_list:
    grouped[elem[1]].append(elem[0])
 
grouped = [lst for lst in grouped if lst]
 
# Printing the result
print("The grouped list is:", grouped)


Output

The grouped list is: [['G', 'F', 'G'], ['I', 'S'], ['B', 'E', 'S', 'T']]

Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(k+n), where k is the number of distinct groups in the input list and n is the length of the input list.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads