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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:53:47+00:00 2026-06-18T15:53:47+00:00

I have a dictionary as: default = {‘a’: [‘alpha’], ‘b’: [‘beta’,’gamma’], ‘g’: []} I

  • 0

I have a dictionary as:

default = {'a': ['alpha'], 'b': ['beta','gamma'], 'g': []}

I wish to eliminate the empty values as:

default = {'a': ['alpha'], 'b': ['beta','gamma']}

I wrote a function (following an example found on the web)

def remove_empty_keys(d):
    for k in d.keys():
        try:
            if len(d[k]) < 1:
                del[k]
        except:
            pass
        return(d)

I have the following questions:

1- I didn’t find the mistake why it always returns following –

remove_empty_keys(default)
 {'a': ['alpha'], 'b': ['beta'], 'g': []}

2- Is there a built-in function to eliminate/delete Null/None/empty values from Python dictionary without creating a copy of the original dictionary?

  • 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-18T15:53:48+00:00Added an answer on June 18, 2026 at 3:53 pm

    To fix your function, change del[k] to del d[k]. There is no function to delete values in place from a dictionary.

    What you are doing is deleting the variable k, not changing the dictionary at all. This is why the original dictionary is always returned.

    Rewritten, your function might look like:

    def remove_empty_keys(d):
        for k in d.keys():
            if not d[k]:
                del d[k]
    

    This assumes you want to eliminate both empty list and None values, and actually removes any item with a “false” value.

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

Sidebar

Related Questions

In my function I have dictionary with empty values: self.items = { 'Maya Executable':
I have some code that sets up a dictionary with some defualt values for
I have several layers of function calls, passing around a common dictionary of key
I used to have the following line in auto-complete : (require 'auto-complete-config) (add-to-list 'ac-dictionary-directories
So, I have the following code: private readonly Dictionary<string, IGame> _gameLookup = new Dictionary<string,
Basically, I have a function that has an optional dictionary argument. Since it's optional,
I have a mutable HashMap and would like to use it like a default-dictionary.
I have a Dictionary that when I add multiple values to it, the items
I have the following code: public enum RoleType { Default = 10, Guest =
Have Dictionary <Int64, byte> that gets used a lot. I mean in a loop

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.