How to remove all strings from a list of tuples python
Use a nested tuple comprehension and isinstance
:
output = [tuple(j for j in i if not isinstance(j, str)) for i in ListTuples]
Output:
[(100,), (80,), (20,), (40,), (40,)]
Note that there are trailing commas in the tuples to distinguish them from e.g. (100)
which is identical to 100
.
Since extracting the first item of each tuple is sufficient, you can unpack and use a list comprehension. For a list of tuples:
res = [(value,) for value, _ in ListTuples] # [(100,), (80,), (20,), (40,), (40,)]
If you need just a list of integers:
res = [value for value, _ in ListTuples] # [100, 80, 20, 40, 40]
For a functional alternative to the latter, you can use operator.itemgetter
:
from operator import itemgetter
res = list(map(itemgetter(0), ListTuples)) # [100, 80, 20, 40, 40]