on-internet
← Community

How to convert strings to the correct numeric type in Python?

Stack Overflow ·

I have a list of numbers as str li = ['1', '4', '8.6'] I want to convert them to [1, 4, 8.6]. If I use int to convert, the result is [1, 4, 8]. If I use float to convert, the result is [1.0, 4.0, 8.6]. I've tried this: li = [1, 4, 8.6] intli = list(map(int, li)) floatli = list(map(float, li)) print(intli) # -> [1, 4, 8] print(floatli) # -> [1.0, 4.0, 8.6]

Comments

Sign in or sign up to comment and upvote.

No comments yet.