How to convert a list of floats to a list of integers in Python

4 Answers

0 votes
lst = [3.56, 4.3, 6.9, 8.5, 9.16]

lst = [int(item) for item in lst]

print(lst)



'''
run:

[3, 4, 6, 8, 9]

'''

 



answered Jul 16, 2022 by avibootz
0 votes
lst = [2.56, 4.3, 6.9, 8.5, 9.16]

lst = [round(item) for item in lst]

print(lst)



'''
run:

[3, 4, 7, 8, 9]

'''

 



answered Jul 16, 2022 by avibootz
0 votes
import math

lst = [2.56, 4.3, 6.9, 8.5, 9.16]

lst = [math.ceil(item) for item in lst]

print(lst)



'''
run:

[3, 5, 7, 9, 10]

'''

 



answered Jul 16, 2022 by avibootz
0 votes
import math

lst = [2.56, 4.3, 6.9, 8.5, 9.16]

lst = [math.floor(item) for item in lst]

print(lst)



'''
run:

[2, 4, 6, 8, 9]

'''

 



answered Jul 16, 2022 by avibootz

Related questions

2 answers 130 views
1 answer 111 views
1 answer 131 views
1 answer 165 views
...