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 6557257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:03:02+00:00 2026-05-25T13:03:02+00:00

I have a list of values – all strings. I want to convert these

  • 0

I have a list of values – all strings. I want to convert these values to their respective datatypes. I have mapping of values to the types information available.

There are three different datatypes: int, str, datetime.
The code needs to be able to handle the error cases with the data.

I am doing something like:-

tlist =  [ 'some datetime value', '12', 'string', .... ]

#convert it to: [ datetime object, 12, 'string', ....]

error_data = ['', ' ', '?', ...]

d = { 0: lambda x: datetime.strptime(x,...) if x not in error_data else x, 
      1: lambda x: int(x) if x not in error_data else 0,
      2: lambda x: x 
      ...
     }

result = [ d[i](j) for i, j in enumerate(tlist) ]

The list to convert is very long, like 180 values and I need to do it for thousands of such lists. The performance of above code is very poor. What is the fastest way to do it?

Thank you

  • 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-05-25T13:03:03+00:00Added an answer on May 25, 2026 at 1:03 pm

    Thank you guys for all those approaches. Yeah, I tried pretty much all the approaches mentioned, but none did perform well.

    I tried the following approach and it worked pretty well for my performance needs. This is what I did.

    1. I handled the datetime values as suggested by utdemir
    2. I’ve inserted value 0 for all the int error values with code like –

      [i] = value if value != '' else 0

    3. Instead of coercing value by value using dictionary, I coerced all the value at once using a list.


      def coerce(l):
      return [ l[0], int(l[1]), int(l[2]) ... ]

    My Observations:

    1. handling the error cases separately and coercing at once helps a lot.
    2. letting the code recover from an exception takes a lot of time.
    3. Going over a list multiple times doesn’t really hurt( I did it for handling error cases)
    4. To give an idea, for couple of hours of data (0.60 million records), my approach brought down the runtime from 6-7 minutes to 2m30s
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to have a list of values in a .properties file, i.e.: my.list.of.strings=ABC,CDE,EFG
I have a List<myObject> and I want to keep all the list values when
I have a list of string values that I want add to a hashtable
I have a list with these values: ['1', '10', '100.3', '1000.4', '1000.42', '150', '200',
I have a list of values and I want to put them in a
I have a list of values I want to insert into a table via
I have a list of values and dates for stock ticker symbols and want
I have a list of values: a = [1,3,4,5,2] I now want the following
I have a list of int values: List<int> histogram; How do I normalize all
Let's say I have a list of values to which I want to apply

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.