In Python, I want to convert all strings in a list to integers.
So if I have:
results = ['1', '2', '3']
How do I make it:
results = [1, 2, 3]
Use int() function to Convert list to int in Python. This method with a list comprehension returns one integer value that combines all elements of the list.
If you want to print a list of integers, you can use a map() function to transform them into strings. Then you can use the join() method to merge them into one string and print them out.
Use the map
function (in Python 2.x):
results = map(int, results)
In Python 3, you will need to convert the result from map
to a list:
results = list(map(int, results))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With