0

What is an optimal way of removing all items containing a number from a large list of strings?

Input: ['This', 'That', 'Those4423', '42', '13b' 'Yes', '2']

Output: ['This', 'That', 'Yes']

SD7Codr
  • 11
  • 2
  • 3

2 Answers2

7
>>> foo = ['This', 'That', 'Those4423', '42', '13b', 'Yes', '2']
>>> foo1 = [x for x in foo if not any(x1.isdigit() for x1 in x)]
>>> foo
['This', 'That', 'Those4423', '42', '13b', 'Yes', '2']
>>> foo1
['This', 'That', 'Yes']
>>>

However you can use .isalpha() to check if the string contains alphabetic characters only.

.isaplha()
 [x for x in foo if x.isalpha()]
Simeon Aleksov
  • 1,275
  • 1
  • 13
  • 25
2

Using a list comprehension:

[element for element in my_list if all(digit not in element for digit in "1234567890")]
L3viathan
  • 26,748
  • 2
  • 58
  • 81