Python – Remove duplicate words from Strings in List

Sometimes, while working with Python list we can have a problem in which we need to perform removal of duplicated words from string list. This can have application when we are in data domain. Let’s discuss certain ways in which this task can be performed.

Method #1 : Using set() + split() + loop
The combination of above methods can be used to perform this task. In this, we first split each list into combined words and then employ set() to perform the task of duplicate removal.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# Remove duplicate words from Strings in List
# using loop + set() + split()
  
# Initializing list
test_list = ['gfg, best, gfg', 'I, am, I', 'two, two, three' ]
  
# printing original list
print("The original list is : " + str(test_list))
  
# Remove duplicate words from Strings<code></code> in List
# using loop + set() + split()
res = []
for strs in test_list:
    res.append(set(strs.split(", ")))
      
# printing result 
print ("The list after duplicate words removal is : " + str(res))

chevron_right


Output :

The original list is : ['gfg, best, gfg', 'I, am, I', 'two, two, three']
The list after duplicate words removal is : [{'best', 'gfg'}, {'I', 'am'}, {'three', 'two'}]

 

Method #2 : Using list comprehension + set() + split()
This is similar method to above. The difference is that we employ list comprehension instead of loops to perform the iteration part.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# Remove duplicate words from Strings in List
# using list comprehension + set() + split()
  
# Initializing list
test_list = ['gfg, best, gfg', 'I, am, I', 'two, two, three' ]
  
# printing original list
print("The original list is : " + str(test_list))
  
# Remove duplicate words from Strings in List
# using list comprehension + set() + split()
res = [set(strs.split(", ")) for strs in test_list]
      
# printing result 
print ("The list after duplicate words removal is : " + str(res))

chevron_right


Output :

The original list is : ['gfg, best, gfg', 'I, am, I', 'two, two, three']
The list after duplicate words removal is : [{'best', 'gfg'}, {'I', 'am'}, {'three', 'two'}]



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.