Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Python | Grouping similar substrings in list - GeeksforGeeks https://www.geeksforgeeks.org/python-grouping-similar-sub...

DSA Data Structures Algorithms Interview Preparation Topic-wise Practice C++ Java

Python | Grouping similar substrings in


list
Last Updated : 17 Jun, 2021

Sometimes we have an application in which we require to group


common prefix strings into one such that further processing can be
done according to the grouping. This type of grouping is useful in the
cases of Machine Learning and Web Development. Let’s discuss certain
ways in which this can be done.
Method #1 : Using lambda + itertools.groupby() + split() 
The combination of above three functions help us achieve the task. The
split method is key as it defines the separator by which grouping has to
be performed. The groupby function does the grouping of elements.

Python3

# Python3 code to demonstrate


# group similar substrings
# using lambda + itertools.groupby() + split()
from itertools import groupby
 
# initializing list
test_list = ['geek_1', 'coder_2', 'geek_4', 'coder_3', 'pro_3']
 
# sort list
# essential for grouping
test_list.sort() ▲

1 of 7 11/4/22, 21:02
Python | Grouping similar substrings in list - GeeksforGeeks https://www.geeksforgeeks.org/python-grouping-similar-sub...

 
# printing the original list
print ("The original list is : " + str(test_list))
 
# using lambda + itertools.groupby() + split()
# group similar substrings
res = [list(i) for j, i in groupby(test_list,
                  lambda a: a.split('_')[0])]
 
# printing result
print ("The grouped list is : " + str(res))

Output : 
The original list is : [‘coder_2’, ‘coder_3’, ‘geek_1’, ‘geek_4’, ‘pro_3’] 
The grouped list is : [[‘coder_2’, ‘coder_3’], [‘geek_1’, ‘geek_4’],
[‘pro_3’]] 

Method #2 : Using lambda + itertools.groupby() + partition() 


The similar task can also be performed replacing the split function with
the partition function. This is more efficient way to perform this task as
it uses the iterators and hence internally quicker.

Python3

# Python3 code to demonstrate


# group similar substrings
# using lambda + itertools.groupby() + partition()
from itertools import groupby
 
# initializing list
test_list = ['geek_1', 'coder_2', 'geek_4', 'coder_3', 'pro_3']
 
# sort list
# essential for grouping
test_list.sort() ▲

2 of 7 11/4/22, 21:02
Python | Grouping similar substrings in list - GeeksforGeeks https://www.geeksforgeeks.org/python-grouping-similar-sub...

 
# printing the original list
print ("The original list is : " + str(test_list))
 
# using lambda + itertools.groupby() + partition()
# group similar substrings
res = [list(i) for j, i in groupby(test_list,
              lambda a: a.partition('_')[0])]
 
# printing result
print ("The grouped list is : " + str(res))

Output : 
The original list is : [‘coder_2’, ‘coder_3’, ‘geek_1’, ‘geek_4’, ‘pro_3’] 
The grouped list is : [[‘coder_2’, ‘coder_3’], [‘geek_1’, ‘geek_4’],
[‘pro_3’]] 

Like 1

Previous Next

Python | Group strings at itertools.groupby() in Python


particular element in list

R ECO M M E N D E D A RT I C L E S Page : 1 2 3

3 of 7 11/4/22, 21:02
Python | Grouping similar substrings in list - GeeksforGeeks https://www.geeksforgeeks.org/python-grouping-similar-sub...

01 Python - Convert List to key-


value list by prefix grouping 05 Python | Frequency grouping of
list elements
30, Jul 20 18, Nov 19

02 Python | Binary element list


grouping 06 Python - Value nested grouping
on List
02, Jan 19 21, Apr 20

03 Python | Identical Consecutive


Grouping in list 07 Python - Summation Grouping
in Dictionary List
02, Sep 19 30, Apr 20

04 Python | Consecutive elements


grouping in list 08 Python - List Elements
Grouping in Matrix
02, Sep 19 29, Aug 20

Article Contributed By :

manjeet_04
@manjeet_04

Vote for di�culty

Easy Normal Medium Hard Expert

Improved By : sweetyty

Article Tags : Python list-programs, Python string-programs, Python,


Python Programs

Practice Tags : python


4 of 7 11/4/22, 21:02
Python | Grouping similar substrings in list - GeeksforGeeks https://www.geeksforgeeks.org/python-grouping-similar-sub...

Improve Article Report Issue

Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.

Load Comments

5 of 7 11/4/22, 21:02
Python | Grouping similar substrings in list - GeeksforGeeks https://www.geeksforgeeks.org/python-grouping-similar-sub...

A-143, 9th Floor, Sovereign Corporate Tower,


Sector-136, Noida, Uttar Pradesh - 201305

feedback@geeksforgeeks.org

Company Learn

About Us Algorithms

Careers Data Structures

In Media SDE Cheat Sheet

Contact Us Machine learning

Privacy Policy CS Subjects

Copyright Policy Video Tutorials

Courses

News Languages
Top News
Python
Technology
Java
Work & Career
CPP
Business
Golang
Finance
C#
Lifestyle
SQL
Knowledge
Kotlin

Web Development Contribute

Web Tutorials Write an Article

Django Tutorial Improve an Article

HTML Pick Topics to Write

JavaScript Write Interview Experience

Bootstrap Internships

ReactJS Video Internship

6 of 7 11/4/22, 21:02
Python | Grouping similar substrings in list - GeeksforGeeks https://www.geeksforgeeks.org/python-grouping-similar-sub...

ReactJS Video Internship

Start Your Coding


NodeJS Journey Now!

@geeksforgeeks , Some rights reserved

7 of 7 11/4/22, 21:02

You might also like