Reputation: 13
I have a list like
['h : abcd', 'i : 467', 'gh578']
I want the output like
['abcd', '467', 'gh578']
How to do it using split?
Upvotes: 0
Views: 28
Reputation: 140188
regular expressions can do that without chained str
calls in a list comprehension:
>>> [re.sub(".*:\s*","",x) for x in a]
['abcd', '467', 'gh578']
basically removing all what's behind colon, and possible spaces after that, to leave only the rest.
Upvotes: 0
Reputation: 1265
Try:
a = ['h : abcd', 'i : 467', 'gh578']
[i.split(':')[-1].strip() for i in a]
it is okay to split gh578
with :
, but the trick is use [-1]
to get the last one, and strip out space.
Upvotes: 1