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

  • Home
  • SEARCH
  • 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 6005395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:23:27+00:00 2026-05-23T01:23:27+00:00

How do I compare two lists of dict ? The result should be the

  • 0

How do I compare two lists of dict? The result should be the odd ones out from the list of dict B.

Example:

ldA = [{'user':"nameA", 'a':7.6, 'b':100.0, 'c':45.5, 'd':48.9},
       {'user':"nameB", 'a':46.7, 'b':67.3, 'c':0.0, 'd':5.5}]


ldB =[{'user':"nameA", 'a':7.6, 'b':99.9, 'c':45.5, 'd':43.7},
      {'user':"nameB", 'a':67.7, 'b':67.3, 'c':1.1, 'd':5.5},
      {'user':"nameC", 'a':89.9, 'b':77.3, 'c':2.2, 'd':6.5}]

Here I want to compare ldA with ldB. It should print the below output.

ldB -> {user:"nameA",  b:99.9, d:43.7}
ldB -> {user:"nameB",  a:67.7, c:1.1 }
ldb -> {user:"nameC", a:89.9, b:77.3, c:2.2, d:6.5}

I have gone through the below link, but there it return onlys the name, but I want name and value like above.

List of Dicts comparision to match between lists and detect value changes 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. Editorial Team
    Editorial Team
    2026-05-23T01:23:27+00:00Added an answer on May 23, 2026 at 1:23 am

    For a general solution, consider the following. It will properly diff, even if the users are out of order in the lists.

    def dict_diff ( merge, lhs, rhs ):
        """Generic dictionary difference."""
        diff = {}
        for key in lhs.keys():
              # auto-merge for missing key on right-hand-side.
            if (key not in rhs):
                diff[key] = lhs[key]
              # on collision, invoke custom merge function.
            elif (lhs[key] != rhs[key]):
                diff[key] = merge(lhs[key], rhs[key])
        for key in rhs.keys():
              # auto-merge for missing key on left-hand-side.
            if (key not not lhs):
                diff[key] = rhs[key]
        return diff
    
    def user_diff ( lhs, rhs ):
        """Merge dictionaries using value from right-hand-side on conflict."""
        merge = lambda l,r: r
        return dict_diff(merge, lhs, rhs)
    
    import copy
    
    def push ( x, k, v ):
        """Returns copy of dict `x` with key `k` set to `v`."""
        x = copy.copy(x); x[k] = v; return x
    
    def pop ( x, k ):
        """Returns copy of dict `x` without key `k`."""
        x = copy.copy(x); del x[k]; return x
    
    def special_diff ( lhs, rhs, k ):
          # transform list of dicts into 2 levels of dicts, 1st level index by k.
        lhs = dict([(D[k],pop(D,k)) for D in lhs])
        rhs = dict([(D[k],pop(D,k)) for D in rhs])
          # diff at the 1st level.
        c = dict_diff(user_diff, lhs, rhs)
          # transform to back to initial format.
        return [push(D,k,K) for (K,D) in c.items()]
    

    Then, you can check the solution:

    ldA = [{'user':"nameA", 'a':7.6, 'b':100.0, 'c':45.5, 'd':48.9},
           {'user':"nameB", 'a':46.7, 'b':67.3, 'c':0.0, 'd':5.5}]
    ldB =[{'user':"nameA", 'a':7.6, 'b':99.9, 'c':45.5, 'd':43.7},
          {'user':"nameB", 'a':67.7, 'b':67.3, 'c':1.1, 'd':5.5},
          {'user':"nameC", 'a':89.9, 'b':77.3, 'c':2.2, 'd':6.5}]
    import pprint
    if __name__ == '__main__':
        pprint.pprint(special_diff(ldA, ldB, 'user'))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I compare the items in two lists and create a new list
I want to compare two lists. Since we code to interface using List ,
I am trying to Compare two lists and keep on each list only items
I need to compare two lists where each list contains about 60,000 objects. what
I have two lists: List<User> collection1 = new List<User>(); List<User> collection2 = new List<User>();
I am trying to compare two lists of data which has some free text
i try to compare two lists with each other: ListA (a1,a2,a3,...) ListB (b1,b2,b3,...) I
Note: Near complete beginner to logic programming I need to compare two lists of
I was wondering how I would compare two 2-dim lists by their location. For
I am trying to compare two files. I will list the two file content:

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.