Convert a List of integers to List of floats in Python

This tutorial will discuss about unique ways to convert a list of integers to list of floats in Python.

Method 1: Using List Comprehension

Iterate over all elements of List usinga for loop inside the `List Comprehension`. During iteration convert each integer to float using the `float()` method, and construct a new list of these floats using `List Comprehension`.

Let’s see the complete example,

```listOfNums = [11, 12, 13, 15, 18, 29]

# convert list of integers to list of floats
listOfFloats = [float(elem) for elem in listOfNums]

print(listOfFloats)
```

Output

```[11.0, 12.0, 13.0, 15.0, 18.0, 29.0]
```

We connverted a List of Integers into a list of floats.

Method 2: Using map() method

Pass the `float()` method, and list of integers to the map() function. It will apply `float()` method on each integer in the list, and store converted float values in a mapped object. Then convert that mapped object into a list of floats using `list()` method.

Let’s see the complete example,

```listOfNums = [11, 12, 13, 15, 18, 29]

# convert list of integers to list of floats
listOfFloats = list(map(float, listOfNums))

print(listOfFloats)
```

Output

```[11.0, 12.0, 13.0, 15.0, 18.0, 29.0]
```

We connverted a List of Integers into a list of floats.

Method 3: Using NumPy Arrays

Create a `NumPy Array` from list of integers, but also pass the `dtype` as `float` while calling the numpy array constructor. It will store all integers from list as floats in the numpy array. Then convert the numpy array of floats into a list of floats. This way list of integers will be converted into a list of floats.

Let’s see the complete example,

```import numpy as np

listOfNums = [11, 12, 13, 15, 18, 29]

# convert list of integers to list of floats
listOfFloats = np.array(listOfNums, dtype=float).tolist()

print(listOfFloats)
```

Output

```[11.0, 12.0, 13.0, 15.0, 18.0, 29.0]
```

Summary

We discussed several ways to convert a list of integers into a list of floats in Python. Thanks.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top