Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading