How do I extract numbers from list of strings

29 Views Asked by At

I have a Python list:

List1 = ["mohit23", "jimmy24"]

I want output as:

[23, 24]
1

There are 1 best solutions below

0
Tim Biegeleisen On

We can try using re.search along with a list comprehension:

List1 = ["mohit23", "jimmy24"]
nums = [int(re.search(r'\d+$', x).group()) for x in List1]
print(nums)  # [23, 24]