Python | Binary Group Tuple list elements

Sometimes, while working with tuples, we can have problems of grouping them, be it based on gender or any particular binary category. This can have applications in many domains. Let’s discuss certain ways in which this can be performed.

Method #1 : Using generator + loop + zip()
The brute force method to perform this task, in this, we perform the combination grouping with the help of zip(), and iteration logic is handled by generator and loop.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Binary Group Tuple list elements
# using generator + loop + zip()
  
# helper function to perform task 
def bin_group(test_list):
     for tup1, tup2 in zip(test_list[0::2], test_list[1::2]):
             yield (tup1[0], tup1[1], tup2[1])
  
# initialize list 
test_list = [(1, 56, 'M'), (1, 14, 'F'), (2, 43, 'F'), (2, 10, 'M')]
  
# printing original list 
print("The original list : " + str(test_list))
  
# Binary Group Tuple list elements
# using generator + loop + zip()
res = list(bin_group(test_list))
  
# printing result
print("The list after binary grouping : " + str(res))

chevron_right


Output :



The original list : [(1, 56, 'M'), (1, 14, 'F'), (2, 43, 'F'), (2, 10, 'M')]
The list after binary grouping : [(1, 56, 14), (2, 43, 10)]

 

Method #2 : Using defaultdict() + list comprehension + sorted() + items()
Combination of above functions can be used to perform this task. In this, we convert the list into dictionary and then perform key based grouping and rearrange back to tuple list.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Binary Group Tuple list elements
# using defaultdict() + list comprehension + sorted() + items()
from collections import defaultdict
  
# initialize list 
test_list = [(1, 56, 'M'), (1, 14, 'F'), (2, 43, 'F'), (2, 10, 'M')]
  
# printing original list 
print("The original list : " + str(test_list))
  
# Binary Group Tuple list elements
# using defaultdict() + list comprehension + sorted() + items()
temp = defaultdict(list)
for ele in test_list:
    temp[ele[0]].append(ele[1])
res = sorted((key, ) + tuple(val) for key, val in temp.items())
  
# printing result
print("The list after binary grouping : " + str(res))

chevron_right


Output :

The original list : [(1, 56, 'M'), (1, 14, 'F'), (2, 43, 'F'), (2, 10, 'M')]
The list after binary grouping : [(1, 56, 14), (2, 43, 10)]


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.