Python | Binary element list grouping

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output:



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

 
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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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]]
  
# using itertools.groupby() + itemgetter()
# to perform binary list grouping
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 result
print ("The grouped list is : " +  str(res))

chevron_right


Output:

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

 
Method #3 : Using collections.defaultdict()

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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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()))

chevron_right


Output:

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.