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

Strip number characters from start of string in list

my_list = [‘1. John’,
‘2. James’,
‘3. Mark’,
‘4. Mary’,
‘5. Helen’,
‘6. David’]

I would like to remove the number that is a string, the "." and the white space before the name.

for i in my_list:
i.lstrip(". ")

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

I was hoping the output would be a list as such:

mylist = [‘John’,
‘James’,
‘Mark’,
‘Mary’,
‘Helen’,
‘David’]

>Solution :

If you really want to use strip, you can try:

my_list = ['1. John', '2. James', '3. Mark', '4. Mary', '5. Helen', '6. David']
name_list = [item.lstrip('123456789. ') for item in my_list]

Or as @Michael Butscher mentioned, to retrieve the name part you can simply use split to split the string by space into two part ['1. ', 'John'] and retrieve the last part:

name_list= [item.split(' ')[-1] for item in my_list]

Result:
['John', 'James', 'Mark', 'Mary', 'Helen', 'David']

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