Hobbies: - Running, Cycling, Rollerblading
I just want to extract the three hobbies: Running, cycling and rollerblading into a list without including in the word hobbies or any of the punctuations : - or space that might be there.
I tried r.split(', ') but it will include Hobbies: - Running. So that is not what i wanted.
CodePudding user response:
You can achieve that by first removing everything else including and on left of hyphen.
str1 = "Hobbies: - Running, Cycling, Rollerblading"
desired = str1.split("-")[1]
res = [i.strip() for i in desired.split(",")]
print(res)
CodePudding user response:
Split by the text before Running if you have this type of text you will have then grab only the second section of the code and finally split by a comma and a space which leaves you with just your words
r.split(': - ').pop(1).split(', ')
(Edit) I include the : in the original split in case you want to use this to also create a list of categories. You can then split this off into 2 commands.
temp = r.split(': - ')
category = temp[0]
listOfItems = temp[1].split(', ')