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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:44:33+00:00 2026-06-08T04:44:33+00:00

I need to access all the non-integer keys for a dict that looks like:

  • 0

I need to access all the non-integer keys for a dict that looks like:

result = { 
           0 : "value 1",
           1 : "value 2",
           "key 1" : "value 1",
           "key 2" : "value 2", 
         }

I am currently doing this by:

headers = [header for header in tmp_dict.keys() if not isinstance(header, int)]

My question:

  1. Is there a way to do this without type checking?
  2. This tmp_dict is coming out of a query using pymssql with the as_dict=True attribute, and for some reason it returns all the column names with data as expected, but also includes the same data indexed by integers. How can I get my query result as a dictionary with only the column values and data?

Thanks for your help!

PS – Despite my issues being resolved by potentially answering 2, I’m curious how this can be done without type checking. Mainly for the people who say “never do type checking, ever.”

  • 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-08T04:44:35+00:00Added an answer on June 8, 2026 at 4:44 am

    With regard to your question about type checking, the duck-type approach would be to see whether it can be converted to or used as an int.

    def can_be_int(obj):
        try:
            int(obj)
        except (TypeError, ValueError):
            return False
        return True
    
    headers = [header for header in tmp_dict.keys() if not can_be_int(header)]
    

    Note that floats can be converted to ints by truncating them, so this isn’t necessarily exactly equivalent.

    A slight variation on the above would be to use coerce(0, obj) in place of int(obj). This will allow any kind of object that can be converted to a common type with an integer. You could also do something like 0 + obj and 1 * obj which will check for something that can be used in a mathematical expression with integers.

    You could also check to see whether its string representation is all digits:

    headers = [header for header in tmp_dict.keys() if not str(header).isdigit()]
    

    This is probably closer to a solution that doesn’t use type-checking, although it will be slower, and it’s of course entirely possible that a column name would be a string that is only digits! (Which would fail with many of these approaches, to be honest.)

    Sometimes explicit type-checking really is the best choice, which is why the language has tools for letting you check types. In this situation I think you’re fine, especially since the result dictionary is documented to have only integers and strings as keys. And you’re doing it the right way by using isinstance() rather than explicitly checking type() == int.

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

Sidebar

Related Questions

I have common functionality that I need to access from all screens of my
So I have 3 Activities that need to all have access to the same
I need to access as String all the single parameters contained in a complex
I need access to the uint64_t typedef from stdint.h in some wrapper code that
I have two different modules that need access to a single file (One will
I know that's bad design, but I need access to the view from my
I need to access a components tag attribute like: <h:inputtext id=input_age/> from a backing
I have an ASP.net Web Site Project (.net 3.5). Currently all of the non-code
I need to remotely load a .NET DLL that contains an ActiveX object (non-visual)
What I need is a key-value container for keeping some well-known parameters of objects.

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.