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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:42:14+00:00 2026-05-23T20:42:14+00:00

If A and B are two dictionaries, using python, is there any way of

  • 0

If A and B are two dictionaries, using python, is there any way of removing elements from Dictionary A that are in dictionary B?

For example,

parent_dict = {"a" : "aaa", "b" : "bbb", "c" : "ccc", "d" : "ddd", "e": "eee"}
derived_dict = {"a" : "aaa", "d" : "ddd", "e" : "eee"}

Now I need to write a function dict_reduce(dictA, dictB) which deletes all the elements of dictB from dictA.

(i.e.,) dict_reduce(parent_dict, derived_dict) should give
{“b” : “bbb”, “c” : “ccc”}

My work around with a for loop is:

def dict_reduce(parent_dict, child_dict):
    for key in child_dict.keys():
        del parent_dict[key]
    return parent_dict

reduced_dict = dict_reduce(parent_dict, child_dict)

NOTE:

  1. It will be great if the solution is a one liner or something which
    do not takes a for loop.
  2. Also we need not to check whether parent
    dictionary has the key before deleting, since the child dictionary
    is derived from the parent dictionary. So need not think about
    keyError.
  3. The parent dictionary is a static dictionary which should not be affected by the method. Instead the returning dictionary should be stored in another reduced dictionary.
  4. There is no need to check whether the child_dict has same key as that of parent_dict. Only the key matters.
  • 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-23T20:42:15+00:00Added an answer on May 23, 2026 at 8:42 pm
    {k: v for k, v in parent_dict.items() if k not in derived_dict}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way in C# to merge two dictionaries? I have two dictionaries
Im new two python and am trying to grow a dictionary of dictionaries. I
What would anyone consider the most efficient way to merge two datasets using Python?
Possible Duplicate: python dict.add_by_value(dict_2) ? My input is two dictionaries that have string keys
I want to merge two CSV files that I have read into python using
If I have two dictionaries I'd like to combine in Python, i.e. a =
I need to compare two dictionaries and to update another dictionary with unmatched items
I'm using Python 2.6, and I have two datasets, each being a list of
I've been using the following to create an array from a plist of dictionaries:
I have a Dictionary of dictionaries : SortedDictionary<int,SortedDictionary<string,List<string>>> I would like to merge two

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.