Use map() method to convert a list of strings to ints in Python. For example, want to convert all strings in a list to integers.
list = ['1', '2', '3']
How do I make it:
results = [1, 2, 3]
Python Example convert list of strings to ints
Simple python example code.
Use map(function, iterable) with a list of strings as iterable and int as function to apply int() to each string in the list. Use list(iterable) with the output of map() as iterable to convert to a list.
string_list = ["1", "2", "3"]
integer_map = map(int, string_list)
Maps each string to an int
integer_list = list(integer_map)
Converts mapped output to a list of ints
print(integer_list)
Output:
Do comment if you have any doubts and suggestions on this python list code.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
Windows 10
Python 3.10.1
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.