1test_list = ['1', '4', '3', '6', '7']
2
3int_list = [int(i) for i in test_list]
1Use the map function (in Python 2.x):
2results = map(int, results)
3
4In Python 3, you will need to convert the result from map to a list:
5results = list(map(int, results))
1# Basic syntax:
2list_of_ints = [int(i) for i in list_of_strings]
3
4# Example usage with list comprehension:
5list_of_strings = ['2', '3', '47']
6list_of_ints = [int(i) for i in list_of_strings]
7print(list_of_ints)
8--> [2, 3, 47]
1# Example usage using list comprehension:
2# Say you have the following list of lists of strings and want integers
3x = [['565.0', '575.0'], ['1215.0', '245.0'], ['1740.0', '245.0']]
4list_of_integers = [[int(float(j)) for j in i] for i in x]
5
6print(list_of_integers)
7--> [[565, 575], [1215, 245], [1740, 245]]
8
9# Note, if the strings don't have decimals, you can omit float()
1test_list = ['1', '4', '3', '6', '7']
2
3# Printing original list
4print ("Original list is : " + str(test_list))
5
6# using naive method to
7# perform conversion
8for i in range(0, len(test_list)):
9 test_list[i] = int(test_list[i])
10
11
12# Printing modified list
13print ("Modified list is : " + str(test_list))
14