
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python Grouping similar substrings in list
In this tutorial, we are going to write a program that groups similar substrings from a list. Let's see an example to understand it more clearly.
Input
strings = ['tutorials-python', 'tutorials-c', 'tutorials-java', 'tutorials-javascript', 'python-1', 'python-2', 'javascript-1']
Output
[['tutorials-python', 'tutorials-c', 'tutorials-java', 'tutorials-javascript'], ['python-1', 'python-2'], ['javascript-1']]
We are going to use the groupby method from itertools module to solve the problem. The groupby method will group all the similar string into an iter object. For the given list we have split the string with - and pass the first part of the string to the groupby method.
Let's see the steps involved in solving this problem.
- Initialize the list of strings.
- Import the itertools module.
- Initialize an empty list.
- Now, pass the strings and a lambda function to the itertools.groupby method.
- The lambda function should return first part of the string after splitting it with −
- The groupby method will return a list of the tuples with the element and its group.
- In every iteration, convert the group of similar elements into a list.
- Append the list to the empty list.
- Print the result.
Example
# importing the itertools module import itertools # initializing the strings strings = ['tutorials-python', 'tutorials-c', 'tutorials-java', 'tutorials-javascript', 'python-1', 'python-2', 'javascript-1'] # empty list result = [] # iterator # lambda function will return first part from the string iterator = itertools.groupby(strings, lambda string: string.split('-')[0]) # iterating over the result # element and its group for element, group in iterator: # appending the group by converting it into a list result.append(list(group)) # printing the result print(result)
Output
If you run the above code, then you will get the following result.
[['tutorials-python', 'tutorials-c', 'tutorials-java', 'tutorials-javascript'], ython-1', 'python-2'], ['javascript-1']]
Conclusion
If you have any doubts in the tutorial, mention them in the comment section.
- Related Articles
- Binary element list grouping in Python
- Python – List Elements Grouping in Matrix
- Program to count number of similar substrings for each query in Python
- Add similar value multiple times in a Python list
- Checking if starting digits are similar in list in Python
- Palindromic Substrings in Python
- Program to find the largest grouping of anagrams from a word list in Python
- Python - Grouping columns in Pandas Dataframe
- Find the Maximum of Similar Indices in two list of Tuples in Python
- Grouping the array items in MongoDB and get the count the products with similar price?
- Python – Extract range of Consecutive similar elements ranges from string list
- How regular expression grouping works in Python?
- Remove Substrings in One Iteration in python
- Python - Find all the strings that are substrings to the given list of strings
- How to List all Substrings in a given String using C#?

Advertisements