Comparing multiple dictionaries in Python

I'm new to Python and am running to a problem I can't google my way out of. I've built a GUI using wxPython and ObjectiveListView. In its very center, the GUI has a list control displaying data in X rows (the data is loaded by the user) and in five columns.

When the user selects multiple entries from the list control (pressing CTRL or shift while clicking), the ObjectiveListView module gives me a list of dictionaries, the dictionaries containing the data in the rows of the list control. This is exactly what I want, good!

The returned list looks something like this:

print MyList
[{'id':1023, 'type':'Purchase', 'date':'23.8.2008', 'sum':'-21,90', 'target':'Apple Store'}, {'id':1024, 'type':'Purchase', 'date':'24.8.2008', 'sum':'-21,90', 'target':'Apple Store'}, {'id':23, 'type':'Purchase', 'date':'2.8.2008', 'sum':'-21,90', 'target':'Apple Store'}]

All the dictionaries have the same keys, but the values change. The 'id' value is unique. Here the problems start. I want to get the common values for all the items the user selected. In the above list they would be 'sum':'-21,90' and 'target':'Apple Store'.

I don't know how to properly compare the dicts in the list. One big problem is that I don't know beforehand how many dicts the list contains, since it's decided by the user.

I have a vague idea that list comprehensions would be the way to go, but I only know how to compare two lists with list comprehensions, not n lists. Any help would be appreciated.


Asked by: Lyndon267 | Posted: 06-10-2021






Answer 1

>>> mysets = (set(x.items()) for x in MyList)
>>> reduce(lambda a,b: a.intersection(b), mysets)
set([('sum', '-21,90'), ('type', 'Purchase'), ('target', 'Apple Store')])

First, I've created a generator that will convert the list of dicts into an iterable sequence of sets of key,value pairs. You could use a list comprehension here but this way doesn't convert your entire list into yet another list, useful if you don't know how big it will be.

Then I've used reduce to apply a function that finds the common values between each set. It finds the intersection of set 1 & set 2, which is itself a set, then the intersection of that set & set 3 etc. The mysets generator will happily feed each set on demand to the reduce function until its done.

I believe reduce has been deprecated as a built-in in Python 3.0, but should still be available in functools.

You could of course make it a one-liner by replacing mysets in the reduce with the generator expression, but that reduces the readability IMO. In practice I'd probably even go one step further and break the lambda out into its own line as well:

>>> mysets = (set(x.items()) for x in MyList)
>>> find_common = lambda a,b: a.intersection(b)
>>> reduce(find_common, mysets)
set([('sum', '-21,90'), ('type', 'Purchase'), ('target', 'Apple Store')])

And if you need the end result to be a dict, just wrap it like so:

>>> dict(reduce(find_common, mysets))
{'sum': '-21,90', 'type': 'Purchase', 'target': 'Apple Store'}

dict can accept any iterator of key,value pairs, such as the set of tuples returned at the end.

Answered by: Victoria913 | Posted: 07-11-2021



Answer 2

My answer is identical to Matthew Trevor's, except for one difference:

>>> mysets = (set(x.items()) for x in MyList)
>>> reduce(set.intersection, mysets)
set([('sum', '-21,90'), ('type', 'Purchase'), ('target', 'Apple Store')])

Here I use set.intersection instead of creating a new lambda. In my opinion this is more readable, as this intuitively reads as "reduce is reducing this list using the set intersection operator." This should also be much faster, as set.intersection is a built-in C function.

To fully answer your question, you can extract the values using a list comprehension:

>>> mysets = (set(x.items()) for x in MyList)
>>> result = reduce(set.intersection, mysets)
>>> values = [r[1] for r in result]
>>> values
['-21,90', 'Purchase', 'Apple Store']

This would end up on one line for me. but that's entirely up to you:

>>> [r[1] for r in reduce(set.intersection, (set(x.items()) for x in myList))]
['-21,90', 'Purchase', 'Apple Store']

Answered by: Lily370 | Posted: 07-11-2021



Answer 3

First, we need a function to compute intersection of two dictionaries:

def IntersectDicts( d1, d2 ) :
    return dict(filter(lambda (k,v) : k in d2 and d2[k] == v, d1.items()))

Then we can use it to process any number of dictionaries:

result = reduce(IntersectDicts, MyList)

Answered by: Lenny289 | Posted: 07-11-2021



Answer 4

Since you're only looking for the common set, you can compare the keys in the first dictionary to the keys in all other dictionaries:

common = {}
for k in MyList[0]:
    for i in xrange(1,len(MyList)):
        if MyList[0][k] != MyList[i][k]: continue
        common[k] = MyList[0][k]

>>> common
{'sum': '-21,90', 'type': 'Purchase', 'target': 'Apple Store'}

Answered by: Kimberly914 | Posted: 07-11-2021



Answer 5

Sorry, yes, 'type':'Purchase' is also one of the common values.Should have logged in to edit the question.

Answered by: Wilson275 | Posted: 07-11-2021



Similar questions

python - Comparing value of same key between multiple dictionaries

I am trying to create a function that will take an arbitrary number of dictionaries as arguments and return the dictionary with the lowest value for the 'qty' key. All dictionaries have the same number of items and are formatted as follows: dictA = {'qty':1000, 'color':'red'} dictB = {'qty':3000, 'color':'red'} dictC = {'qty':5000, 'color':'red'} ... I know how to return the ...


python - Comparing multiple dictionaries from JSON files?

Right now I have a JSON file that looks like this: "suggested":[{ "city": "Berlin", "location": "Europe", "hotels": true, "restaurants": true, "rivers": false}, { "city": "Andorra", "location": "Global", "hotels": false, "restaurants": true, "rivers": true }] And I want to compare tha...


python - Comparing multiple values per key in two dictionaries?

I am trying to compare two dictionaries, one dictionary contains multiple keys that are assigned to different SSIDs in the surrounding area, the ssid contains numerous values such as bssid, channel, security, etc. My other dictionary contains just one key thats assigned to SSID with the same numerous values. What I want to do is individually compare the scanned_ap values of a duplicate ssid with the current network...


python - comparing two dictionaries with list type values

I'm trying to compare two dictionaries. My approach is to turn them into two separate lists of tuples and to then use the set module. Here is an illustration: dict = {'red':[1,2,3],'blue':[2,3,4],'green':[3,4,5]} dict1 = {'green':[3,4,5],'yellow':[2,3,4],'red':[5,2,6]} intersection = set(set(dict.items()) & set(dict1.items())) apparently, this is comparing two lists of tuples and pyt...


python - Comparing lists of dictionaries

I have two lists of test results. The test results are represented as dictionaries: list1 = [{testclass='classname', testname='testname', testtime='...},...] list2 = [{testclass='classname', testname='testname', ...},...] The dictionary representation is slightly different in both lists, because for one list I have some more information. But in all cases, every test dictionary in either ...


python - Comparing Keys and Values in 2 Dictionaries

I want to take the aggregate numbers populated in one dictionary and compare both the keys and the values against keys and values in another dictionary to determine differences between the two. I can only conclude something like the following: for i in res.keys(): if res2.get(i): print 'match',i else: print i,'does not match' for i in res2.keys(): if res.get(i): print ...


python - Comparing Two Dictionaries Key Values and Returning the Value If Match

I'm a beginner to Python, but I've been trying this syntax and I cannot figure it out -- which was been really baffling. crucial = {'eggs': '','ham': '','cheese': ''} dishes = {'eggs': 2, 'sausage': 1, 'bacon': 1, 'spam': 500} if crucial.keys() in dishes.keys(): print dishes[value] What I want to do is -- if crucial has a key (in this case, eggs) in the dishes, it will re...


python - Pythonic way of comparing multiple elements in a list of dictionaries

I have a list of dictionaries in Python. Each element of the list has a type key with the element's type. Something like this: e1 = {"type": 1, "value": 23.1} e2 = {"type": 1, "value": 21.1} e3 = {"type": 2, "value": -10.1} e4 = {"type": 1, "value": -2.59} l = [e1, e2, e3, e4] I would like to know if all elements in list l are of the same ty...


python - Comparing value from a nested dictionaries and list

I want to compare values from two variables (dictionary and list). Dictionary has a nested construct, so I have to loop over the all items. I discovered simple solution, but I am pretty sure that I can do this in a better way (using python). In a brief I want to find items from user_from_database which are not existing in a user_from_client variable. My solution:


mysql - Comparing 2 dictionaries in python by lookup

I have 2 lists dictionaries that i have created that have "similar" data in them, basically it works like this: dictionary 1 looks like this: {'category' : category, 'location' : location, 'count' : count} dictionary 2 looks like this: {'category' : category, 'location' : location, 'average' : average, 'create_datetime' : create_datetime} Basically...


json - Comparing python dictionaries and find diffrence of the two

So im trying to write a python program that will take 2 .json files compare the contents and display the differences between the two. So far my program takes user input to select two files and compares the two just fine. I have hit a wall trying to figure out how to print what the actual differences are between the two files. my program: #!/usr/bin/env python2 import json #get_json() requests us...


python - Another solution to comparing lists, dictionaries or sets? I've tried everything

I have data within a sql database where each row has a unique identifier and a bunch of codes that are separated by commas. What I want to do is make a list where each identifier has only one code per row. Which I've done after importing data: rows = cur.fetchall() for row in rows: (pid, codelist) = row codes = set() for x in codelist.split(','): if len(x.strip()) > 0: codes.add(x.strip())...


python - Comparing values in two dictionaries when keys are different

I need to compare values between two dictionaries with DIFFERENT keys, and put the results in the third dictionary. Input: a={1:[0,0], 2:[1,0], 3:[1,2]} b={4:[1,2], 5:[1,3]} Output: I want to have two out dictionaries, both of them will have keys copied from 'b'. In all cases I would like to ignore entries '0'. The first output shows how many keys in 'a' had the exact same value as key in ...


variables - How to maintain lists and dictionaries between function calls in Python?

I have a function. Inside that I'm maintainfing a dictionary of values. I want that dictionary to be maintained between different function calls Suppose the dic is : a = {'a':1,'b':2,'c':3} At first call,say,I changed a[a] to 100 Dict becomes a = {'a':100,'b':2,'c':3} At another call,i changed a[b] to 200 I want that dic to be a = {'a':100,'b':200,'c':3}


dictionary - Comparing dictionaries in Python

Given two dictionaries, d1 and d2, and an integer l, I want to find all keys k in d1 such that either d2[k]<l or k not in l. I want to output the keys and the corresponding values in d2, except if d2 does not contain the key, I want to print 0. For instance, if d1 is a: 1 b: ...


python - Map two lists into one single list of dictionaries

Imagine I have these python lists: keys = ['name', 'age'] values = ['Monty', 42, 'Matt', 28, 'Frank', 33] Is there a direct or at least a simple way to produce the following list of dictionaries ? [ {'name': 'Monty', 'age': 42}, {'name': 'Matt', 'age': 28}, {'name': 'Frank', 'age': 33} ]


python - Can I get rows from SQLAlchemy that are plain arrays, rather than dictionaries?

I'm trying to optimize some Python code. The profiler tells me that SQLAlchemy's _get_col() is what's killing performance. The code looks something like this: lots_of_rows = get_lots_of_rows() for row in lots_of_rows: if row.x == row.y: print row.z I was about to go through the code and make it more like this... lots_of_rows = get_lots_of_rows() for row in lots_...


python - Elegant, pythonic solution for forcing all keys and values to lower case in nested dictionaries of Unicode strings?

I'm curious how the Python Ninjas around here would do the following, elegantly and pythonically: I've got a data structure that's a dict from unicode strings to dicts from unicode strings to unicode string lists. So: >>> type(myDict) <type 'dict'> >>> type(myDict[u'myKey']) <type 'dict'> >>> type(myDict[u'myKey'][u'myKey2']) <type 'list'> >>> ty...


python - Creating dictionaries with pre-defined keys

In python, is there a way to create a class that is treated like a dictionary but have the keys pre-defined when a new instance is created?


Dictionaries with volatile values in Python unit tests?

I need to write a unit test for a function that returns a dictionary. One of the values in this dictionary is datetime.datetime.now() which of course changes with every test run. I want to ignore that key completely in my assert. Right now I have a dictionary comparison function but I really want to use assertEqual like this: def my_func(self): return {'monkey_head_count': 3, 'monke...


dictionary - "Adding" Dictionaries in Python?

This question already has answers here:


python - Sorting a list of dictionaries of objects by dictionary values

This is related to the various other questions about sorting values of dictionaries that I have read here, but I have not found the answer. I'm a newbie and maybe I just didn't see the answer as it concerns my problem. I have this function, which I'm using as a Django custom filter to sort results from a list of dictionaries. Actually, the main part of this function was answered in a related question on stackoverfl...


dictionary - item frequency in a python list of dictionaries

Ok, so I have a list of dicts: [{'name': 'johnny', 'surname': 'smith', 'age': 53}, {'name': 'johnny', 'surname': 'ryan', 'age': 13}, {'name': 'jakob', 'surname': 'smith', 'age': 27}, {'name': 'aaron', 'surname': 'specter', 'age': 22}, {'name': 'max', 'surname': 'headroom', 'age': 108}, ] and I want the 'frequency' of the items within each column. So for this I'd get something like:...






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top