Us86
Us86

Reputation: 13

Splitting list items to get only the values after the delimiter

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

Answers (2)

Jean-François Fabre
Jean-François Fabre

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

LiuXiMin
LiuXiMin

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

Related Questions