Here are 3 approaches to convert strings to integers in a Python list:
(1) Using a map function:
list_of_strings = ["value1", "value2", "value3", ...]
list_of_integers = list(map(int, list_of_strings))
print(list_of_integers)
For example:
list_of_strings = ["22", "45", "75", "68", "39"]
list_of_integers = list(map(int, list_of_strings))
print(list_of_integers)
The result:
[22, 45, 75, 68, 39]
(2) Using a List Comprehension:
list_of_strings = ["value1", "value2", "value3", ...]
list_of_integers = [int(i) for i in list_of_strings]
print(list_of_integers)
For example:
list_of_strings = ["22", "45", "75", "68", "39"]
list_of_integers = [int(i) for i in list_of_strings]
print(list_of_integers)
The result:
[22, 45, 75, 68, 39]
(3) Use a For Loop:
list_of_strings = ["value1", "value2", "value3", ...]
list_of_integers = []
for i in list_of_strings:
list_of_integers.append(int(i))
print(list_of_integers)
For example:
list_of_strings = ["22", "45", "75", "68", "39"]
list_of_integers = []
for i in list_of_strings:
list_of_integers.append(int(i))
print(list_of_integers)
The result:
[22, 45, 75, 68, 39]