I am looking to “clear” the list by excluding any elements containing characters other than 0-9, and I wonder if there is a more efficient way than, for example,
import re
invalid = re.compile('[^0-9]')
ls = ['1a', 'b3', '1']
cleaned = [i for i in ls if not invalid.search(i)]
print cleaned
>> ['1']
How am I going to work with large lists (5 thousand items) of long strings (15 characters).
source
share