Using all with a generator expression, you do not need to count, compare the length:
>>> [i for i in x if all(j.isdigit() or j in string.punctuation for j in i)] ['12,523', '3.46']
By the way, the above and OP code will contain strings containing only punctuation.
>>> x = [',,,', '...', '123', 'not number'] >>> [i for i in x if all(j.isdigit() or j in string.punctuation for j in i)] [',,,', '...', '123']
To handle this, add another condition:
>>> [i for i in x if all(j.isdigit() or j in string.punctuation for j in i) and any(j.isdigit() for j in i)] ['123']
You can do this a little faster by storing the result of string.punctuation in a set.
>>> puncs = set(string.punctuation) >>> [i for i in x if all(j.isdigit() or j in puncs for j in i) and any(j.isdigit() for j in i)] ['123']
source share