get numbers from a list ofg strings python code example
Example 1: how to extract numbers from a list in python
a = ['1 2 3', '4 5 6', 'invalid']
numbers = []
for item in a:
for subitem in item.split():
if(subitem.isdigit()):
numbers.append(subitem)
print(numbers)
['1', '2', '3', '4', '5', '6']
Example 2: extract numbers from list of strings python using regex
new_list = [int(item) for sublist in a for item in sublist if item.isdigit()]