Reputation: 1697
I have a list of values in this format:
['11111-11111','22222-22222','33333-33333']
How do I strip the values in the list so I only have the first 5 numbers of each value in the list?
Looking for:
['11111','22222','33333']
Upvotes: 0
Views: 157
Reputation: 706
If you would like a very simple solution, it could be like that:
yourList = ['11111-11111','22222-22222','33333-33333']
newList = []
for item in yourList:
newList.append(item[:5])
print(newList)
The output is ['11111', '22222', '33333']
Upvotes: 1
Reputation: 394239
You can use a list comprehension and slice each element in your list:
In [16]:
l=['11111-11111','22222-22222','33333-33333']
[x[:5] for x in l]
Out[16]:
['11111', '22222', '33333']
a call to str.split
would work too:
In [17]:
l=['11111-11111','22222-22222','33333-33333']
[x.split('-')[0] for x in l]
Out[17]:
['11111', '22222', '33333']
Upvotes: 7