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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:50:02+00:00 2026-05-11T08:50:02+00:00

I have a problem with combining or calculating common/equal part of these two dictionaries.

  • 0

I have a problem with combining or calculating common/equal part of these two dictionaries. In my dictionaries, values are lists:

d1 = {0:['11','18','25','38'],        1:['11','18','25','38'],        2:['11','18','25','38'],        3:['11','18','25','38']}  d2 = {0:['05','08','11','13','16','25','34','38','40', '43'],        1:['05', '08', '09','13','15','20','32','36','38', '40','41'],        2:['02', '08', '11', '13', '18', '20', '22','33','36','39'],        3:['06', '11', '12', '25', '26', '27', '28', '30', '31', '37']} 

I’d like to check ‘d2’ and know if there are numbers from ‘d1’. If there are some, I’d like to update one of them with new data or receive 3rd dictionary ‘d3’ with only the values that are identical/equal in both ‘d1’ and ‘d2’ like:

d3 = {0:['11','25','38'], 1:['38'], 2:['11','18'], 3:['11','25']}  

Can anyone help me with this?

My fault I forgot to be more specific. I’m looking for a solution in Python.

  • 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. 2026-05-11T08:50:03+00:00Added an answer on May 11, 2026 at 8:50 am

    Assuming this is Python, you want:

    dict((x, set(y) & set(d1.get(x, ()))) for (x, y) in d2.iteritems()) 

    to generate the resulting dictionary ‘d3’.

    Python 3.0+ version

    >>> d3 = {k: list(set(d1.get(k,[])).intersection(v)) for k, v in d2.items()} {0: ['11', '25', '38'], 1: ['38'], 2: ['11', '18'], 3: ['11', '25']} 

    The above version (as well as Python 2.x version) allows empty intersections therefore additional filtering is required in general case:

    >>> d3 = {k: v for k, v in d3.items() if v} 

    Combining the above in one pass:

    d3 = {} for k, v in d2.items():     # find common elements for d1 & d2     v3 = set(d1.get(k,[])).intersection(v)     if v3: # whether there are common elements        d3[k] = list(v3)  

    [Edit: I made this post community wiki so that people can improve it if desired. I concede it might be a little hard to read if you’re not used to reading this sort of thing in Python.]

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

Sidebar

Related Questions

Having a problem combining 2 JOINS. Both statements below work. I have also included
how can i write a select query by combining IN and LIKE i have
I have a problem with jQuqeryUI. I would like to run some code whenever
I am combining div toggling with complex forms: http://railscasts.com/episodes/75-complex-forms-part-3 My specific situation: I have
I have a problem when compiling shared libraries and executables using emacs on Windows
I have a problem with compiling an Oracle trigger via SQL*PLUS - I don't
I have problem with $_POST data. Some fields are missing from input form when
I have problem with Android using the class HttpUrlConnection my method is this: public
I have problem thinking this trough with PHP & Mysql . I have read
I have problem in some JavaScript that I am writing where the Switch statement

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.