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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:13:28+00:00 2026-06-01T05:13:28+00:00

This question is inspired by this question . I’d like to get a dictionary

  • 0

This question is inspired by this question. I’d like to get a dictionary from a list of dictionaries that should contain all key/value pairs from all dictionaries that are either only contained once, or where all dictionaries agree on the associated value. Example (taken from the aforementioned posting):

dicts = [dict(a=3, b=89, d=2), dict(a=3, b=89, c=99), dict(a=3, b=42, c=33)]
print dict_itersection(dicts)

should yield

{'a': 3, 'd': 2}

My current implementation looks like this:

import collections

def dict_intersection(dicts):
        c=collections.defaultdict(set)
        for d in dicts:
                for a, b in d.iteritems():
                        c[a].add(b)
        return {a: next(iter(b)) for a, b in c.iteritems() if len(b) == 1}

So my question: Can this be done more elegantly?

Sidequestion: can next(iter(b)) be done better without modification of the underlying dictionary (i.e. not b.pop())?

  • 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-01T05:13:30+00:00Added an answer on June 1, 2026 at 5:13 am

    All solutions so far assume that all dictionary values are hashable. Since the code won’t get slower and only little more complex without this assumption, I’d drop it. Here’s a version that works for all values that support !=:

    def dict_intersection(dicts):
        result = {}
        conflicting = set()
        for d in dicts:
            for k, v in d.iteritems():
                if k not in conflicting and result.setdefault(k, v) != v:
                    del result[k]
                    conflicting.add(k)
        return result
    

    The set conflicting will only contain dictionary keys, which will always be hashable.

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

Sidebar

Related Questions

Inspired by the discussion in this question . We have all been taught that
Inspired by the comments on this question , I'm pretty sure that Java String
This question is inspired from Joel's Making Wrong Code Look Wrong http://www.joelonsoftware.com/articles/Wrong.html Sometimes you
This is directly inspired by this question . There are numerous references/statements that bitwise
Inspired by this question. Why is there no list.clear() method in python? I've found
This question was inspired by this answer to another question, indicating that you can
This question is actually inspired from another one here on SO and I wanted
This is more a conceptual question. It's inspired from using some extremely large table
This question is inspired from the accepted answer here . Why does the following
This question is inspired from another topic which poses this question: Find the first

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.