0

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 Jane', 'My name is Lopez']

Desired output:

final_lst1 = ['bag', 'cat', 'dog']
final_lst2 = ['Cola', 'Sprite']
final_lst3 = ['James', 'Mary Jane', 'Lopez'] 
4
  • 1
    A better way to phrase this might be to say that you want to extract all fruit names. What is the exact problem statement here? Commented Oct 17, 2021 at 10:35
  • Do you know the repeated substring beforehand, e. g. is it defined in a variable? Or must it be detected from the list items? Commented Oct 17, 2021 at 10:40
  • @MichaelButscher Hi, the repeated substring must be detected from the list items. Thanks! Commented Oct 17, 2021 at 11:14
  • Is the common string always the beginning of the string? Commented Oct 17, 2021 at 18:04

5 Answers 5

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Here, you can separate out the unique words for any given list, i.e., For a list where the repeated substring is different for each list.
0

This is a solution to the question asked:

final_lst = [s.replace('State your favorite fruit: ') for s in lst]

Comments

0

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']

Comments

0

you can split on ":" and get last index like below:

[x.split(":")[-1] for x in lst]

Comments

0

You can iterate trough the list and then take the last word from each string:

final_lst = [w.split(" ")[-1] for w in lst]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.