'Is there a way I can convert a list of string into a list of int in python/flask? [duplicate]
I have this list of strings:
data = ["23444,2345,5332,2534,3229"]
Is there a way I can convert it to list of int, like this:
[23444,2345,5332,2534,3229]
Solution 1:[1]
A simple list comprehension should do it:
data = ["23444,2345,5332,2534,3229"]
newdata = [int(v) for v in data[0].split(',')]
print(newdata)
...or with map...
newdata = list(map(int, data[0].split(',')))
Output:
[23444, 2345, 5332, 2534, 3229]
Solution 2:[2]
Create a variable to hold your new values and simply loop through the values and convert each to int.
For example
strings = ["2","3","4","5","6"]
ints = []
for i in strings:
ints.append(int(i))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Albert Winestein |
| Solution 2 | Maxwell D. Dorliea |
