Strip number characters from start of string in list

Advertisements

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(". ")

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']

Leave a ReplyCancel reply