How can i remove every character before a specific character in a tuple of lists of strings in Python

I have a tuple of lists of strings that looks like this

myList = ['https://wwww.example.com/watch/random-video-12'], ['https://wwww.example.com/watch/random-video-t-14'], ['https://www.example.com/watch/random-video-longest-1']

and i need to remove the last "-" character and every character before that.

>Solution :

result = []
for url in myList:
    result.append(url[0].split('-')[-1])
print(result)

Outputs :

['12', '14', '1']

Explanation :

url is a list that has a single element, url[0]. Applying split('-') on that string split the string into a list of strings that were separated with ‘-‘.
ie : 'https://wwww.example.com/watch/random-video-12'.split('-') -> ['https://wwww.example.com/watch/random', 'video', '12'].
Finally list[-1] gives the last element of list

Leave a Reply