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.