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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:53:06+00:00 2026-06-12T06:53:06+00:00

I want a dictionary class that implements an intersection_update method, similar in spirit to

  • 0

I want a dictionary class that implements an intersection_update method, similar in spirit to dict.update but restricting the updates only to those keys that are already present in the calling instance (see below for some example implementations).

But, in the spirit of Wheel Reinvention Avoidance, before I go off implementing (and writing tests for, etc.) a mapping class with this additional functionality, does anything like this already exist in a more-or-less standard module?


To be clear, the intersection_update method I have in mind would do something like this:

def intersection_update(self, other):
    for k in self.viewkeys() & other.viewkeys():
        self[k] = other[k]

…although an actual implementation may attempt some possible optimizations, like, e.g.:

def intersection_update(self, other):
    x, y = (self, other) if len(self) < len(other) else (other, self)
    for k in x.iterkeys():
        if k in y:
            self[k] = other[k]

Edit: In the original version of this post I had written “Alternatively, is there a standard Python idiom that obviates the need to implement a [class with a intersection_update] method?”, but I deleted almost immediately it because, upon further reflection, I realized that was an invitation for weak answers, since I know enough of the “core” of the Python language to be pretty certain that no such idiom exists, at least not one that would match the advantages (generality, legibility, ease of typing) of a dedicated method.

  • 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-12T06:53:08+00:00Added an answer on June 12, 2026 at 6:53 am

    Try this:

    def dict_intersection(d1, d2):
        return dict((key, d2[key] or d1[key]) for key in frozenset(d1) & frozenset(d2))
    

    Or, for python versions >= 2.7:

    def dict_intersection(d1, d2):
        return {key: d2[key] or d1[key] for key in d1.viewkeys() & d2.viewkeys()}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to create an uebersimple class that implements get enumerator, but failing madly due
I'm writing a bijective dictionary class , but I want to ensure the two
If I have a class that implements ICustomTypeDescriptor I can override the GetProperties() method
I have a class that I want to have opportunities to serialize/deserialize. It implements
I want to write a custom class that behaves like dict - so, I
I want to write in Delphi (2009 - so I have generic dictionary class)
i want to sort dictionary based upon value of each dictioanry item.but if i
Suppose I have a class T that I want to use as a key
I'm creating a class which populates a dictionary as a private member. I want
I'm trying to serialize a class that inherits from a base class that implements

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.