I would like to remove the repeated substring from a list of strings. *Assuming that the repeated substring is different for each list.
Example:
lst = ['State your favorite fruit: Apple', 'State your favorite fruit: Orange', 'State your favorite fruit: Grapes']
Desired outcome:
final_lst = ['Apple', 'Orange', 'Grapes']
Edit: Sorry if my initial question was not clear. I hope to find the unique words from each list of strings.
lst1 = ['This is a bag', 'This is a cat', 'This is a dog']
lst2 = ['Favorite drink: Cola', 'Favorite drink: Sprite']
lst3 = ['My name is James', 'My name is Mary', 'My name is Lopez']
Desired output:
final_lst1 = ['bag', 'cat', 'dog']
final_lst2 = ['Cola', 'Sprite']
final_lst3 = ['James', 'Mary', 'Lopez']
CodePudding user response:
This is a solution to the question asked:
final_lst = [s.replace('State your favorite fruit: ') for s in lst]
CodePudding user response:
Just use list comprehension:
lst = ['State your favorite fruit: Apple', 'State your favorite fruit: Orange', 'State your favorite fruit: Grapes']
final_lst = [s.replace('State your favorite fruit: ', '') for s in lst]
print(final_lst)
Output:
['Apple', 'Cherry', 'Grapes']
CodePudding user response:
you can split on ":" and get last index like below:
[x.split(":")[-1] for x in lst]
CodePudding user response:
There might be certain other ways to do this but this below one works just fine for the purpose
So, your list is as below:
lst = ['State your favorite fruit: Apple', 'State your favorite fruit: Orange', 'State your favorite fruit: Grapes']
Now, separate all the words in to a new list
seperate_words = (" ".join(lst)).split(" ") #First we join all the sentences of the list
# with a space in between using the "join" method of string.
#Then consequently splitting the list by a space
Finally, to get the unique words, use the list comprehension as below
unique_words = [word for word in seperate_words if seperate_words.count(word) == 1]
print(unique_words)
Output:['Apple', 'Orange', 'Grapes']
Regards