# How to Convert an Integer List to a Float List in Python

The most Pythonic way to convert a list of integers `ints` to a list of floats is to use the list comprehension expression `floats = [float(x) for x in ints]`. It iterates over all elements in the list `ints` using list comprehension and converts each list element `x` to a float value using the `float(x)` built-in function.

This article shows you the simplest ways to convert a one-dimensional list consisting only of integers to a list of floats.

Problem: Given a list of integers `[1, 2, 3]`. How to convert it to a list of floats `[1.0, 2.0, 3.0]`?

## Method 1: List Comprehension

Suppose we have a list:

`a = [4, 3, 2, 1, 10, 14, -14]`

Now, check the type of the first list element:

```print(type(a))
# <class 'int'>```

Let’s apply the built-in function `float()`, and get a list of floats using list comprehension:

```print([float(x) for x in a])
# [4.0, 3.0, 2.0, 1.0, 10.0, 14.0, -14.0]```

💡 List comprehension is a compact way of creating lists. The simple formula is `[expression + context]`. Expression: What to do with each list element? Context: What elements to select? The context consists of an arbitrary number of `for` and `if` statements.

You can watch me explain list comprehensions in this video:

Check the type of numbers in the new list:

```A = [float(x) for x in a]
print(type(A))
# <class 'float'>```

The built-in function `float()` converts an integer to a float. Thus, it helps us create a new list of floats from the list of integers in a single line of code.

🌍 Recommended Tutorial: How to Convert an Integer to a List in Python?

## Method 2: Map Function

The built-in function `map` is well optimized and efficient, when it is called, the elements of the list are retrieved upon access. Therefore, one element is stored and processed in memory, which allows the program not to store the entire list of elements in the system memory.

Apply to the same list `a` the following code:

```a = [4, 3, 2, 1, 10, 14, -14]
print(list(map(float, a)))
# [4.0, 3.0, 2.0, 1.0, 10.0, 14.0, -14.0]```

? The `map()` function applies the first argument, a function, to each element in an iterable. It transforms each element in the original iterable to a new element and returns a new iterable `map` object of transformed values. To obtain a list, you need to convert it using the built-in `list()` constructor.

You can watch my explainer video of the map function here:

## Method 3: For Loop

Of course, you can also convert a list of ints to a list of floats using a simple for loop. This is what most people coming from a programming language such as Java and C++ would do as they don’t know the most Pythonic way of using list comprehension, yet (see Method 1).

```a = [4, 3, 2, 1, 10, 14, -14]
floats = []

for element in a:
floats.append(float(element))

print(floats)
# [4.0, 3.0, 2.0, 1.0, 10.0, 14.0, -14.0]```

This basic method to convert a list of ints to a list of floats uses three steps:

• Create an empty list with `floats = []`.
• Iterate over each integer element using a `for` loop such as `for element in list`.
• Convert the int to a float using `float(element)` and append it to the new float list using the `list.append()` method.

## Method 4: String Formatting for Custom String Conversions

If this is not enough for you, for instance, you need a specific format of the converted strings such as only two digits after the decimal point, you should have a look at Python’s powerful string formatting capabilities.

For example, to convert a list of ints to a list of strings with only two digits, use the `string.format()` method:

```a = [4, 3, 2, 1, 10, 14, -14]
floats = ['{:.2f}'.format(x) for x in a]
print(floats)
# ['4.00', '3.00', '2.00', '1.00', '10.00', '14.00', '-14.00']```

Python’s built-in `format(value, spec)` function transforms input of one format into output of another format defined by you. Specifically, it applies the format specifier `spec` to the argument `value` and returns a formatted representation of `value`. For example, `format(42, 'f')` returns the string representation `'42.000000'`.

You can watch me introducing the formatting capabilities in this short guide: