Q: How to remove multiple substrings at the end of a list of strings in Python? I have a list of strings:
lst =['puppies com', 'company abc org', 'company a com', 'python limited']

If at the end of the string there is the word
limited, com or org I would like to remove it. How can I go about doing this?
I have tried;
for item in lst:
   j= item.strip('limited')
   j= item.strip('org')

I've also tried the replace function with no avail.
Thanks

A: You can use this example to remove selected last words from the list of string:
lst =['dont strip this', 'puppies com', 'company abc org', 'company a com', 'python limited']
to_strip = {'limited', 'com', 'org'}

out = []
for item in lst:
    tmp = item.rsplit(maxsplit=1)
    if tmp[-1] in to_strip:
        out.append(tmp[0])
    else:
        out.append(item)

print(out)

Prints:
['dont strip this', 'puppies', 'company abc', 'company a', 'python']


A: If i understand this correctly you always want to remove the last word in each sentance?
If that's the case this should work:
lst =['puppies com', 'company abc org', 'company a com', 'python limited']

for i in lst:
    f = i.rsplit(' ', 1)[0]
    print(f)

Returns:
puppies
company abc
company a
python


rsplit is a shorthand for "reverse split", and unlike regular split
works from the end of a string. The second parameter is a maximum
number of splits to make - e.g. value of 1 will give you two-element
list as a result (since there was a single split made, which resulted
in two pieces of the input string). As described here

This is also available in the python doc here.
