Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7731375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:27:50+00:00 2026-06-01T06:27:50+00:00

I have about 50 million lists of strings in Python like this one: [1,

  • 0

I have about 50 million lists of strings in Python like this one:

["1", "1.0", "", "foobar", "3.0", ...]

And I need to turn these into a list of floats and Nones like this one:

[1.0, 1.0, None, None, 3.0, ...]

Currently I use some code like:

def to_float_or_None(x):
    try:
        return float(x)
    except ValueError:
        return None

result = []
for record in database:
    result.append(map(to_float_or_None, record))

The to_float_or_None function is taking in total about 750 seconds (according to cProfile)… Is there a faster way to perform this conversion from a list of strings to a list of floats/Nones?

Update
I had identified the to_float_or_None function as the main bottleneck. I can not find a significant difference in speed between using map and using list comprehensions.
I applied Paulo Scardine’s tip to check the input, and it already saves 1/4 of the time.

def to_float_or_None(x):
    if not(x and x[0] in "0123456789."):
        return None
    try:
        return float(x)
    except:
        return None

The use of generators was new to me, so thank you for the tip Cpfohl and Lattyware! This indeed speeds up the reading of the file even more, but I was hoping to save some memory by converting the strings to floats/Nones.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T06:27:51+00:00Added an answer on June 1, 2026 at 6:27 am

    The answers given thus far don’t really fully answer the question. try...catch vs a validating if then can result in different performance (see: https://stackoverflow.com/a/5591737/456188). To summarize that answer: depends on the ratio of failures to successes and the MEASURED time of a failure and success in both cases. Basically we can’t answer this, but we can tell you how to:

    1. Look at a few representative cases to get a ratio.
    2. Write an if/then that tests the same as the try/catch optimize it and then measure how long it takes both version of the to_float_or_None to fail 100 times and measure how long it takes both versions of the to_float_or_None to succeed 100 times.
    3. Do a little math to figure out which will be faster.

    Side note about the list comprehension issue:

    Depending on whether the you want to be able to index the results of this, or whether you just want to iterate over it a generator expression would actually be even better than a list comprehension (just replace the [ ] characters with ( ) characters).

    It takes essentialy no time to create, and the actual execution of to_float_or_None (which is the expensive part) can be delayed until the result it needed.

    This is useful for many reasons, but won’t work if you’re going to need to index it. It will however, allow you to zip the original collection with the generator so you can still have access to the original string along with its float_or_none result.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of strings containing about 7 million items in a text
I have a list of objects, this list contains about 4 million objects. there
I have a list of about 500 million items. I am able to serialize
I need to delete about 2 million rows from my PG database. I have
I need to load about 6 million objects into a Dictionary. The problem I
I have about 1 million data in users table. Now I want each user
We have about half a million images residing in the file system. Multiple images
I have about 40 workbooks with 1000+ columns and near 1 million records. Unfortunately,
I have a simple script that pulls about 100 million results, 1 million at
We have a non-profit web site that got about 5 million hits in May.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.