0

I want to convert string list in int list in python3.x

b = ["1.22","1.45","1.85","2.35","3.73","5.44"]
c= [int(i) for i in b]
print(c)

But I am getting the error

ValueError: invalid literal for int() with base 10: '1.22'

What is the error in syntax or possible other solution?

Aakash Patel
  • 111
  • 7

3 Answers3

4

1.22 is not an integer. Use decimal.Decimal('1.23') or float('1.23') instead.

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
2

These are floats not ints, that's why you get the error, you could modify your code as such:

b = ["1.22","1.45","1.85","2.35","3.73","5.44"]
c = [float(i) for i in b]
print(c)
solid.py
  • 2,782
  • 5
  • 23
  • 30
2

Try first converting to floating point, then convert those floats to ints:

b = ["1.22","1.45","1.85","2.35","3.73","5.44"]
c = [int(float(i)) for i in b]
print(c)

This prints:

[1, 1, 1, 2, 3, 5]
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360