Reputation: 13
I would like to take a list containing integers in strings and remove those integers. Example:
list = ["1. car","2. laptop","3. phone","4. monitor"]
Becomes:
list = ["car","laptop","phone","monitor"]
Upvotes: 0
Views: 98
Reputation: 42143
If your the numbers in your strings are always at the start and followed by a period and a space, you can do it using the split method:
L = ["1. car","2. laptop","3. phone","4. monitor"]
L = [s.split(". ",1)[-1] for s in L]
print(L) # ['car', 'laptop', 'phone', 'monitor']
Upvotes: 1
Reputation: 25
The .isalpha()
method can be used:
from string import digits
new_list = [''.join(x for x in i if x.isalpha()) for i in list]
Upvotes: 1
Reputation: 13929
Try using list comprehension with re.sub()
:
import re
lst = ["1. car", "2. laptop", "3. phone", "4. monitor"] # avoid using list as a variable name, as list is a built-in function.
output = [re.sub(r'\d+\. ', '', x) for x in lst]
print(output) # ['car', 'laptop', 'phone', 'monitor']
The regex can be (or must be) modified according to your full data.
Upvotes: 2