Given a list of integers, generate the list of the corresponding floats.

Dani AI

Generated

In modern Python 3, the simplest way to convert a list of ints to floats is to materialize a new list from the iterator returned by map. This preserves order and length one-for-one: floats = list(map(float, ints)). If you want to replace the original list in place (useful when other code holds references to it), assign to a full slice: ints[:] = list(map(float, ints)). Note that map returned a list in Python 2, but returns an iterator in Python 3, which is why the list(...) wrapper is important. See map and float in the official docs.

Be aware of precision implications: Python int is arbitrary-precision, but float is a 64-bit IEEE-754 value with about 53 bits of precision. Very large integers (abs(value) > 2**53) will lose exactness when converted. Also remember that bool is a subclass of int, so True becomes 1.0 and False becomes 0.0. For background, see Floating Point Arithmetic: Issues and Limitations.

If your next step is numeric computing, consider converting to a NumPy array for performance and vectorized operations: arr = np.array(ints, dtype=float); convert back with arr.tolist() if needed. See the NumPy quickstart. For cases requiring exact decimal arithmetic (e.g., money), use decimal.Decimal instead of binary floats; see the decimal module.

Recommended Answers

All 8 Replies

you can use a list comprehension:

new_list = [float(integral) for integral in old_list]

hi scru,
it wud b more helpful if u can explain the above sol for this scenario
suppose if i give
>>> l1=[1,2,3,4,5]
>>> l1
[1, 2, 3, 4, 5]
i want l1 to print [1.0,2.0,3.0.....](in float)

Then you would do:

l1 = [1,2,3,4,5]
l2 = [float(i) for i in l1]
print l2

[float(i) for i in l1] is what is known as a list comprehension. You should be already familiar with the for loop which loops through each integer in the list and assigns it to the variable i. The first expression simply tells the comprehension what value to append to the new list; the value here is float(i). So essentially, the comprehension loops through each integer in the list, converts it to a float, and then appends it to a new one which is the assigned to l2.

Just an observation, I would avoid using variable names like l1, l2 and so on, they look a lot like numbers 11, 12.

Here is an example how to use Python function map():

q = [1, 2, 3, 4, 5]

# map applies float() to every element in the list q
qf = map(float, q)

print(qf)  # [1.0, 2.0, 3.0, 4.0, 5.0]

thanks scru and ene... :D

i think i've to go through basics well..and which book do u think is best and easy to learn for beginners in python??

There are a flock of badly written and rather outdated Python books on the market. I would go for some of the free online books.

Swaroop C.H. has rewritten his excellent beginning Python tutorial for Python30:

How to Think Like a Computer Scientist - Learning with Python
written by a college professor, a high school teacher, and a professional programmer:
http://www.thinkpython.com
Foreword by David Beazley, University of Chicago:
http://www.greenteapress.com/thinkpython/html/foreword.html

Above updated: "Think like a Python Programmer" (PDF format):

I really like the text we used for my high-school level class: Python Programming for the Absolute Beginner. Two caveats -- it's game-oriented, and it doesn't use Python 3.0.

That said, it contains clear explanations and models reasonably good programming practices.

Jeff

I second jrcagle's recommendation. I used it in high school too. It's a nice lighthearted approach that explains the major concepts of object-oriented programming fairly well, as well as a pretty good introduction to Python itself.

I remember actually looking forward to reading ahead with this book in my free time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.