Python | Duplicate substring removal from list
Sometimes we can come to the problem in which we need to deal with certain strings in a list which are separated by some separator and we need to remove the duplicates in each of this kind of strings. Simple shorthands to solve this kind of problems is always good to have. Let’s discuss certain ways in which this can be done.
Method #1 : Using set() + split()
This particular problem can be solved using the split function to have target string and then set that actually would remove the duplicacy from the string.
# Python3 code to demonstrate # removing duplicate substrings # using set() + split() # initializing list test_list = [ 'aa-aa-bb', 'bb-cc', 'gg-ff-gg', 'hh-hh'] # printing original list print("The original list : " + str(test_list)) # using set() + split() # removing duplicate substrings res = [set(sub.split('-')) for sub in test_list] # print result print("The list after duplicate removal : " + str(res)) |
The original list : ['aa-aa-bb', 'bb-cc', 'gg-ff-gg', 'hh-hh']
The list after duplicate removal : [{'aa', 'bb'}, {'cc', 'bb'}, {'gg', 'ff'}, {'hh'}]
Method #2 : Using {} + split() + list comprehension
For the cases in which we require to fully segregate the strings as a separate component, we can use these set of methods to achieve this task. The curly braces convert to set and rest all the functionality is similar to method above.
# Python3 code to demonstrate # removing duplicate substrings # using {} + split() + list comprehension # initializing list test_list = [ 'aa-aa-bb', 'bb-cc', 'gg-ff-gg', 'hh-hh'] # printing original list print("The original list : " + str(test_list)) # using {} + split() + list comprehension # removing duplicate substrings res = list({i for sub in test_list for i in sub.split('-')}) # print result print("The list after duplicate removal : " + str(res)) |
The original list : ['aa-aa-bb', 'bb-cc', 'gg-ff-gg', 'hh-hh'] The list after duplicate removal : ['cc', 'ff', 'aa', 'hh', 'gg', 'bb']
Recommended Posts:
- Python | Altering duplicate values from given list
- Python | Duplicate element indices in list
- Python | Removing duplicate dicts in list
- Python | Filter list of strings based on the substring list
- Python | Remove unordered duplicate elements from a list
- Python | Replace substring in list of strings
- Python | Get the substring from given string using list slicing
- Python | Finding strings with given substring in list
- Python | Check if substring is part of List of Strings
- Python | Remove tuples having duplicate first value from given list of tuples
- Python | Remove duplicate tuples from list of tuples
- Using Counter() in Python to find minimum character removal to make two strings anagram
- Python PIL | ImageChops.duplicate()
- Python | Remove duplicate dictionaries from nested dictionary
- Python | Find keys with duplicate values in dictionary
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.



