Python List of Floats

One of the most common tasks I handle as a Python developer is managing collections of decimal numbers. Whether I am building a financial dashboard for a Wall Street firm or a weather tracking app for the Midwest, I almost always reach for a list of floats.

A list of floats in Python is simply a collection where every item is a floating-point number. These are numbers with a decimal point, like 3.14 or 99.99.

In my experience, lists are the “Swiss Army Knife” of Python. They are flexible, easy to change, and perfect for holding everything from stock prices to GPS coordinates.

How to Create a List of Floats in Python

The simplest way to create a list of floats is to define it manually. I do this all the time when I have a small, fixed set of data points to work with.

You just need to wrap your decimal numbers in square brackets and separate them with commas.

# Daily stock prices for a US tech company
stock_prices = [150.25, 152.10, 149.85, 155.00, 153.45]

print(stock_prices)
# Output: [150.25, 152.1, 149.85, 155.0, 153.45]

I find this manual method works best for configuration files or small datasets where I know the exact values beforehand.

Create a List of Floats Using List Comprehension

When I need to generate a list of floats based on existing data or a range of numbers, I use list comprehension. It is a one-line trick that makes the code look much cleaner.

For example, if I have a list of prices in a different format and I want to ensure they are all floats, I use this method.

# Sample transaction amounts from a retail store
raw_data = [20, 45, "12.50", 100]

# Converting all items to floats for calculation
formatted_prices = [float(item) for item in raw_data]

print(formatted_prices)
# Output: [20.0, 45.0, 12.5, 100.0]

You can see the output in the screenshot below.

python list of floats

I prefer this approach because it’s faster to write and easier for other developers on my team to read during a code review.

Use the map() Function for Float Conversion

If you are coming from a background in functional programming, you might like the map() function. I often use it when I need to apply the float() constructor to every item in a large list.

It’s very efficient, especially when you are pulling data from a CSV file where numbers often start as strings.

# Average temperatures (Fahrenheit) for cities in California
temp_strings = ["72.5", "68.4", "75.0", "70.2"]

# Mapping the float function to the list
city_temps = list(map(float, temp_strings))

print(city_temps)
# Output: [72.5, 68.4, 75.0, 70.2]

You can see the output in the screenshot below.

float list

In my ten years of coding, I’ve noticed that map() is sometimes slightly faster than list comprehension for very basic conversions, though the difference is usually negligible for small apps.

Generate a Range of Floats with NumPy

One thing I learned early on is that Python’s built-in range() function doesn’t work with floats. If you try range(0.0, 1.0, 0.1), Python will throw an error.

To get around this, I always use the NumPy library. It has a function called arange (and another called linspace) that handles decimals perfectly.

import numpy as np

# Creating a range of mortgage rates from 5.0% to 6.0% with 0.2% increments
mortgage_rates = np.arange(5.0, 6.2, 0.2).tolist()

print(mortgage_rates)
# Output: [5.0, 5.2, 5.4, 5.6, 5.8, 6.0]

You can see the output in the screenshot below.

float python

I rely on NumPy whenever I’m doing heavy data analysis or scientific computing because it’s much more powerful than standard Python lists.

Essential Methods for Manipulating Float Lists

Once you have your list, you’ll likely need to update it. I find myself using the following four methods almost daily.

Adding and Inserting Elements

If I get a new data point, like a new gas price from a station in Texas, I use append() to add it to the end. If I need it at a specific spot, I use insert().

gas_prices = [3.15, 3.22, 3.10]

# Adding a new price to the end
gas_prices.append(3.25)

# Inserting a price at the second position (index 1)
gas_prices.insert(1, 3.18)

print(gas_prices)
# Output: [3.15, 3.18, 3.22, 3.1, 3.25]

Calculations: Sum, Min, and Max

Working with floats usually means doing some math. Python has built-in functions that make finding the total or the extremes very simple.

# Quarterly revenue in millions (USD)
quarterly_rev = [2.5, 3.8, 1.9, 4.2]

total_revenue = sum(quarterly_rev)
best_quarter = max(quarterly_rev)
lowest_quarter = min(quarterly_rev)

print(f"Total: {total_revenue}, Max: {best_quarter}, Min: {lowest_quarter}")

I use these constantly when generating reports for my clients to give them a quick snapshot of their data.

Deal with Precision Issues

One “gotcha” I always warn junior developers about is float precision. Computers sometimes have trouble representing decimals perfectly in binary.

For example, 0.1 + 0.2 might result in 0.30000000000000004 instead of exactly 0.3.

To handle this in a professional app, I use the round() function or the decimal module for financial calculations.

# Rounding a list of percentages to 2 decimal places
raw_percentages = [0.123456, 0.987654, 0.555555]
clean_percentages = [round(p, 2) for p in raw_percentages]

print(clean_percentages)
# Output: [0.12, 0.99, 0.56]

In this tutorial, I’ve covered the most effective ways to create and manage lists of floats in Python. I’ve found that mastering these basics is the key to writing reliable, professional-grade code.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.