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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:48:52+00:00 2026-06-03T05:48:52+00:00

How do I filter a nested dictionary in python based on key values: d

  • 0

How do I filter a nested dictionary in python based on key values:

d = {'data': {'country': 'US', 'city': 'New York', 'state': None},
     'tags': ['US', 'New York'],
     'type': 'country_info',
     'growth_rate': None
     }

I want to filter this dictionary to eliminate NoneType values so the resulting dict should be:

d = {'data': {'country': 'US', 'city': 'New York'},
     'tags': ['US', 'New York'],
     'type': 'country_info',
     }

Also, the dict can have multiple levels of nesting. I want to remove all NoneType values from the dict.

  • 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-03T05:48:54+00:00Added an answer on June 3, 2026 at 5:48 am

    You can define this recursively pretty easily with a dict comprehension.

    def remove_keys_with_none_values(item):
        if not hasattr(item, 'items'):
            return item
        else:
            return {key: remove_keys_with_none_values(value) for key, value in item.items() if value is not None}
    

    Recursion isn’t too optimised in Python, but given the relatively small number of nestings that are likely, I wouldn’t worry.

    Looking before we leap isn’t too Pythonic, I think it is a better option than catching the exception – as it’s likely that the value will not be a dict most of the time (it is likely we have more leaves than branches).

    Also note that in Python 2.x, you probably want to swap in iteritems() for items().

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

Sidebar

Related Questions

i filter the intent like this <intent-filter> <action android:name=android.nfc.action.NDEF_DISCOVERED/> <category android:name=android.intent.category.DEFAULT/> <data android:mimeType=text/plain />
Need to call a filter function on some options based on a radio selected
I'm trying to filter by a list of values using the criteria API. I
How to filter nested/child collections of entities when including them in EF? Example: Let's
I've made a view with an exposed filter. This filter is taxonomy based, and
i was wondering if it was possible to filter which action is invoked based
I'm having trouble filtering hierarchical data that's being displayed in nested xaml templates. I've
I want to filter out several key phrases out of a dataset. Unfortunately, the
I have an object model where I'm checking a filter value of nested child
I currently have a project for data storage records. There is a nested resource,

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.