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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output :

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output :

The original list : ['aa-aa-bb', 'bb-cc', 'gg-ff-gg', 'hh-hh']
The list after duplicate removal : ['cc', 'ff', 'aa', 'hh', 'gg', 'bb']


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.