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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:09:33+00:00 2026-06-06T20:09:33+00:00

I have 2 dictionary which contain the same keys but the value pairs are

  • 0

I have 2 dictionary which contain the same keys but the value pairs are different. Let’s make dictA and dictB represent the two dictionaries in question.

dictA = {'key1':'Joe', 'key2':'Bob'}
dictB = {'key1':'Smith', 'key2':'Johnson'}

Currently, I am creating a new dictionary based the common occurring keys through a nested if statement. In doing so, the values that share a key are contained within a list, in the new dictionary. See this done below:

dictAB = {}  # Create a new dictionary

# Create a list container for dictionary values
for key in dictA.keys():
    dictAB[key] = []

# Iterate through keys in both dictionaries
# Find matching keys and append the respective values to the list container
for key, value in dictA.iteritems():
    for key2, value2 in dictB.iteritems():
        if key == key2:
            dictAB[key].append(value)
            dictAB[key].append(value2)
        else:
            pass

How can this be made into a more clean structure using python dictionary comprehension?

  • 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-06T20:09:35+00:00Added an answer on June 6, 2026 at 8:09 pm

    Use sets or key views (python 2.7):

    dictAB = {k: [dictA[k], dictB[k]] for k in dictA.viewkeys() & dictB.viewkeys()}
    

    Before 2.7:

    dictAB = dict((k, [dictA[k], dictB[k]]) for k in set(dictA) & set(dictB))
    

    In python 3, you can use the .keys method for such operations directly, as they are implemented as views:

    dictAB = {k: [dictA[k], dictB[k]] for k in dictA.keys() & dictB.keys()}
    

    Demo (python 2.7):

    >>> dictA = {'key1':'Joe', 'key2':'Bob'}
    >>> dictB = {'key1':'Smith', 'key2':'Johnson'}
    >>> dictAB = {k: [dictA[k], dictB[k]] for k in dictA.viewkeys() & dictB.viewkeys()}
    >>> print dictAB
    {'key2': ['Bob', 'Johnson'], 'key1': ['Joe', 'Smith']}
    

    The & operator on either two sets or on a dict view creates the intersection of both sets; all keys that are present in both sets.

    By using an intersection of the keys, this code will work even if either dictA or dictB has keys that do not appear in the other dictionary. If you are absolutely sure the keys will always match, you could just iterate over either dict directly without the intersection:

    dictAB = {k: [dictA[k], dictB[k]] for k in dictA}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 2 lists, both of which contain same number of dictionaries. Each dictionary
I have the following City class. Each city object contains a dictionary which keys
I have two different databases that contain two tables with the exact same design.
I have a list which contains a dictionary of ExpandoObjects. Im binding this to
I have a list which contains dictionaries. Each dictionary has an attribute description which
I have a simple text dictionary file, which contains words, separated by ';'.My problem
I have a Dictionary which I am comparing to another Dictionary (variables typed as
I have a dictionary which is keyed by a List: private Dictionary<List<custom_obj>, string> Lookup;
I have a static dictionary which holds authentications for a web service. I'm running
I have a dictionary object which i would like to encrypt, then put it

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.