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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:15:53+00:00 2026-06-01T01:15:53+00:00

In Python, I want to convert a list of strings: l = [‘sam’,’1′,’dad’,’21’] and

  • 0

In Python, I want to convert a list of strings:

l = ['sam','1','dad','21']

and convert the integers to integer types like this:

t = ['sam',1,'dad',21]

I tried:

t = [map(int, x) for x in l]

but is showing an error.

How could I convert all intable strings in a list to int, leaving other elements as strings?

My list might be multi-dimensional. A method which works for a generic list would be preferable:

l=[['aa','2'],['bb','3']]

  • 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-01T01:15:55+00:00Added an answer on June 1, 2026 at 1:15 am

    I’d use a custom function:

    def try_int(x):
        try:
            return int(x)
        except ValueError:
            return x
    

    Example:

    >>> [try_int(x) for x in  ['sam', '1', 'dad', '21']]
    ['sam', 1, 'dad', 21]
    

    Edit: If you need to apply the above to a list of lists, why didn’t you converted those strings to int while building the nested list?

    Anyway, if you need to, it’s just a matter of choice on how to iterate over such nested list and apply the method above.

    One way for doing that, might be:

    >>> list_of_lists = [['aa', '2'], ['bb', '3']]
    >>> [[try_int(x) for x in lst] for lst in list_of_lists]
    [['aa', 2], ['bb', 3]]
    

    You can obviusly reassign that to list_of_lists:

    >>> list_of_lists = [[try_int(x) for x in lst] for lst in list_of_lists]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like convert strings to floats using Python 2.6 and later, but without silently
I want to convert a list object into a string and insert this string
I want to convert, in python, special characters like %$!&@á é © and not
Is there something existing in python that can convert an increasing list of integers
I want to convert this C++ code to Python v2: static unsigned char asConvCode[3]
I am new to Python and want convert this small JavaScript code to Python.
If I want to convert a float to an integer in Python, what function
I want to convert a python boolean into JS's boolean literal. This is what
I had written a program in Python 3, but now want to convert it
I want a python function that takes a pdf and returns a list of

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.