Q:

make list of string to list of int python

test_list = list(map(int, test_list)) 
1
# Example usage using list comprehension:
# Say you have the following list of lists of strings and want integers
x = [['565.0', '575.0'], ['1215.0', '245.0'], ['1740.0', '245.0']]
list_of_integers = [[int(float(j)) for j in i] for i in x]

print(list_of_integers)
--> [[565, 575], [1215, 245], [1740, 245]]
1
# initializing list  
test_list = ['1', '4', '3', '6', '7'] 

# using list comprehension to 
# perform conversion 
test_list = [int(i) for i in test_list]
1
>>> example_string = '0, 0, 0, 11, 0, 0, 0, 0, 0, 19, 0, 9, 0, 0, 0, 0, 0, 0, 11'
>>> list(map(int, example_string.split(',')))  # Python 3, in Python 2 the list() call is redundant
[0, 0, 0, 11, 0, 0, 0, 0, 0, 19, 0, 9, 0, 0, 0, 0, 0, 0, 11]
>>> [int(s) for s in example_string.split(',')]
[0, 0, 0, 11, 0, 0, 0, 0, 0, 19, 0, 9, 0, 0, 0, 0, 0, 0, 11]
0
test_list = ['1', '4', '3', '6', '7'] 
  
# Printing original list 
print ("Original list is : " + str(test_list)) 
  
# using naive method to 
# perform conversion 
for i in range(0, len(test_list)): 
    test_list[i] = int(test_list[i]) 
      
  
# Printing modified list  
print ("Modified list is : " + str(test_list)) 
-1
[int(s) for s in example_string.split(',')]
0

New to Communities?

Join the community