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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:32:31+00:00 2026-06-13T00:32:31+00:00

I am fairly new to dictionaries, so this is probably a fairly basic question.

  • 0

I am fairly new to dictionaries, so this is probably a fairly basic question.

Let’s say I have two different dictionaries with similar elements.
Example:

    Dictionary1 = {'Bob' : 1, 'Mary' : 2, 'Sue' : 3, 'George' : 4}
    Dictionary2 = {'Bob' : 1, 'Sue' : 2, 'Jill' : 3, 'Isaac' : 4, 'George' : 5}

I want to be able to take the intersection of the two dictionaries and apply the indices of the first dictionary to the second. So I want an output that looks something like this:

    DictionaryCombo = {'Bob' : 1, 'Sue' : 3, 'George' : 4}

Please excuse my formatting for my desired output, as I am not certain what it should look like, though I know that I want the key and value pairs of the intersection of the two dictionaries.

  • 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-13T00:32:32+00:00Added an answer on June 13, 2026 at 12:32 am

    If you want to subset d1 so it only has elements present in d2

    d1 = {'Bob': 1, 'Mary': 2, 'Sue': 3, 'George': 4}
    d2 = {'Bob': 101, 'Sue': 102, 'Jill': 103, 'Isaac': 104, 'George': 105}
    {k: v for k, v in d1.items() if k in d2}
    # {'Bob': 1, 'Sue': 3, 'George': 4}
    

    Or you mention apply, so did you want to update values in d2?

    d2.update(d1)
    print d2
    # {'Sue': 3, 'Mary': 2, 'Jill': 103, 'Isaac': 104, 'Bob': 1, 'George': 4}
    

    Or, if you really are starting with two lists (and not starting with dict’s):

    el1 = ['Bob', 'Mary', 'Sue', 'George']
    el2 = ['Bob', 'Sue', 'Jill', 'Isaac', 'George']
    dict( (val, idx) for idx, val in enumerate(d1, start=1) if val in set(el2) )
    #{'Bob': 1, 'Sue': 3, 'George': 4}
    

    If you have two lists, one of keys, and one of values, and wish to make them a dict, then you can use zip:

    keys = ['Bob', 'Mary', 'Sue', 'George']  
    vals = [1, 2, 3, 4]
    dict( zip(keys, vals) )
    # {'Bob': 1, 'Mary': 2, 'Sue': 3, 'George': 4}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Fairly new to backbone, so this is a really basic question. I have a
Fairly new to python, forgive me if this is a basic question about learning
Im fairly new to c sorry if this is a stupid question! i have
Being fairly new to C++ I have a question bascially concerning the g++ compiler
Being fairly new to Spring I have a question about annotating a class. When
Fairly new to wpf, using the galasoft mvvm templates. I have two relaycommands that
Im fairly new to NodeJS(using c9.io) and have this sick obsession with it lately.
I am new to fairly new to AS3 and I have found myself needing
I am fairly new to php and mysql but I have created a small
I'm fairly new to C++ standard library and have been using standard library lists

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.