-3

I have two lists of dictionaries:

list_1 = [
           {'total': 18, 'lead_status': '2'},
           {'total': 18, 'lead_status': '9'}, 
           {'total': 18, 'lead_status': '8'}, 
           {'total': 16, 'lead_status': '15'}, 
           {'total': 17, 'lead_status': '14'}
         ]

list_2 = [
           {'total': 18, 'lead_status': '2'}, 
           {'total': 22, 'lead_status': '9'},
           {'total': 18, 'lead_status': '8'},
           {'total': 16, 'lead_status': '15'}, 
           {'total': 17, 'lead_status': '14'}
         ]

lead_status always have unique value and the order of dictionary in lists might or might not be same.

I want to check that for each lead_status the total value is same or not in both lists

Example

For lead_status : '2' both the lists have same total which is 18 then it returns True

For lead_status : '9' both the lists have different total which is 18 in list_1 but 22 in list_2. So it returns False.

I have tried the answer in this solution: Comparing 2 lists consisting of dictionaries with unique keys in python

Please help to solve this problem. Any help is appreciated.

Amit Tripathi
  • 7,003
  • 6
  • 32
  • 58
Shubham Srivastava
  • 1,190
  • 14
  • 28

1 Answers1

1

From what I understood from your question, this should work:

In [25]: dict_1 = {l['lead_status']:l['total'] for l in list_1}

In [26]: dict_2 = {l['lead_status']:l['total'] for l in list_2}

In [28]: {k: (dict_2[k] == v) for k, v in dict_1.items()}
Out[28]: {'14': True, '15': True, '2': True, '8': True, '9': False}

This first creates a dict of key equal to the value lead_status and the value of total and then compares the dict created from both the lists.

Though, in case your 'lead_status' key have the same value than that gets overwritten.

Amit Tripathi
  • 7,003
  • 6
  • 32
  • 58
  • This is exactly I want.. You got my point... So can you help me to edit this question. So that the chances for more -ve vote will be less – Shubham Srivastava Dec 14 '17 at 19:16
  • ```lead_status``` key will be unique in my case... Thanks... :) – Shubham Srivastava Dec 14 '17 at 19:16
  • @ShubhamSrivastava Glad, it helped :) I have edited your question to show an example of [how to ask a good question on stackoverflow](https://stackoverflow.com/help/how-to-ask). Always, show what have you tried. Paste the minimum but all required part of the code with any stack trace. This helps the community in answering the question. – Amit Tripathi Dec 14 '17 at 19:24
  • Thanks. I will be much more clear next time. :) – Shubham Srivastava Dec 14 '17 at 19:26