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

The Archive Base Latest Questions

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

I want to take two dictionaries and print a diff of them. This diff

  • 0

I want to take two dictionaries and print a diff of them. This diff should include the differences in keys AND values. I’ve created this little snippet to achieve the results using built-in code in the unittest module. However, it’s a nasty hack since I have to subclass unittest.TestCase and provide a runtest() method for it to work. In addition, this code will cause the application to error out since it will raise an AssertError when there are differences. All I really want is to print the diff.

import unittest
class tmp(unittest.TestCase):
    def __init__(self):
         # Show full diff of objects (dicts could be HUGE and output truncated)
        self.maxDiff = None
    def runTest():
        pass
_ = tmp()
_.assertDictEqual(d1, d2)

I was hoping to use the difflib module, but it looks to only work for strings. Is there some way to work around this and still use difflib?

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

    You can use difflib, but the use unittest method seems more appropriate to me. But if you wanted to use difflib. Let’s say say the following are the two dicts.

    In [50]: dict1
    Out[50]: {1: True, 2: False}
    
    In [51]: dict2
    Out[51]: {1: False, 2: True}
    

    You may need to convert them to strings (or list of strings) and then go about using difflib as a normal business.

    In [43]: a = '\n'.join(['%s:%s' % (key, value) for (key, value) in sorted(dict1.items())])
    In [44]: b = '\n'.join(['%s:%s' % (key, value) for (key, value) in sorted(dict2.items())])
    In [45]: print a
    1:True
    2:False
    In [46]: print b
    1:False
    2:True
    In [47]: for diffs in difflib.unified_diff(a.splitlines(), b.splitlines(), fromfile='dict1', tofile='dict2'):
        print diffs
    

    THe output would be:

    --- dict1
    
    +++ dict2
    
    @@ -1,2 +1,2 @@
    
    -1:True
    -2:False
    +1:False
    +2:True
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to take two lists and find the values that appear in both.
I want to take two tables and just put them next to each other
I want to take two values from string 105.106 The values 105 and 106
Essentially, what I want to do (in DirectX) is to take two partially-transparent images
I have two histograms (freq dist) and I want to take the average of
I want to take a math expression that takes variables and print (assign to
I want to take the values if the variable I define are not equal
I want to take two times (in seconds since epoch) and show the difference
I want the user to enter two numbers with a space between them, then
I basically want to take two images taken from the camera on the iPhone

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.