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>>> example_string = '0, 0, 0, 11, 0, 0, 0, 0, 0, 19, 0, 9, 0, 0, 0, 0, 0, 0, 11'
2>>> list(map(int, example_string.split(','))) # Python 3, in Python 2 the list() call is redundant
3[0, 0, 0, 11, 0, 0, 0, 0, 0, 19, 0, 9, 0, 0, 0, 0, 0, 0, 11]
4>>> [int(s) for s in example_string.split(',')]
5[0, 0, 0, 11, 0, 0, 0, 0, 0, 19, 0, 9, 0, 0, 0, 0, 0, 0, 11]
6
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