I have some math expressions in a list, like ["2", "*", "2"]
, ["3", "+", "pi"]
. Now, I want the list elements that contain ONLY digits to be detected, and be converted to integers. Is there a way to do this?
Asked
Active
Viewed 87 times
0

WarpPrime
- 215
- 5
- 20
1 Answers
-1
newList = [int(i) for i in oldList if i.isdigit()]

Just for fun
- 4,102
- 1
- 5
- 11
-
1You don't need to call `str(i)` on strings. `i.isdigit()` will work fine. – Jasmijn Aug 09 '20 at 15:16
-
I did that thinking if there will be an int in list beforehand. like [1,'*','2'] – Just for fun Aug 09 '20 at 15:18