Compare two dictionaries

Hi there,

I’m trying to compare two dictionaries vie Python script, but the Dynamo return me ‘false’ all the time, why? I’ve tried this algorithm in PyCharm and it is functional but not in Dynamo Python Node, is this a limitation of IronPython? Thank a lost for your thought and ideas.
-lt

you just wanna map two keys and check if the values are the same right?? try the code below

#Preparing input from dynamo to revit
xls_data = {"A":10, "B":40, "C":20}
rvt_data = [{"A":10, "B":40, "C":20}, {"A":20, "B":40, "C":10}, {"A":10, "B":30, "C":30}]

#Do some action in a Transaction
finalchecklist = list()
for dict in rvt_data:
	keys = sorted(dict.Keys)
	checklist = list()
	for key in keys:
		if xls_data[key] == dict[key]: checklist.append(True)
		else: checklist.append(False)
	finalchecklist.append(checklist)
#Final output
OUT = finalchecklist

1 Like

Perhaps the data types stored within each dictionary is different? One might have strings whereas the other has integers, therefore returning false when comparing 10 to "10".

1 Like

Hi guys,

thanks for your time, unfortunately I didn’t find a way how to return a dictionary. It seems that a solution is to return a list, but that is not what I have wanted. I have checked if both of the inputs have the same data type and they have, so I don’t know where the dog is buried, anyway thanks a lot once again…

-lt

Yes I believe that is correct :frowning: maybe this thread is useful…

You could output the key value pairs and recreate the dictionary in Dynamo?

Cheers,

Mark

I think the two (Dynamo dictionary and python dictionary) are not the same, although you expect them to be interchangeable.
I work around this by returning a list of two lists [keys,values] and then use the node dictionary for keys and values .
Jonathan

Hello
if you want only one result by compare 2 dictionarry you can use set Python Operation

xls_data = {"A":10, "B":40, "C":20}
rvt_data = [{"A":10, "B":40, "C":20}, {"A":None, "B":None, "C":None}, {"A":20, "B":40, "C":10}, {"A":10, "B":30, "C":30}]

out = []
for rvt_dict in rvt_data:
	comp = set(xls_data.items()) - set(rvt_dict.items())
	if comp:
		out.append(False)
	else:
		out.append(True)	
	
OUT = out

ahh you wanted to return a dict. okay then i think the below should work for you.

#Preparing input from dynamo to revit
xls_data = {"A":10, "B":40, "C":20}
rvt_data = [{"A":10, "B":40, "C":20}, {"A":20, "B":40, "C":10}, {"A":10, "B":30, "C":30}]

#Do some action in a Transaction
finalchecklist = list()
for dict in rvt_data:
	keys = sorted(dict.Keys)
	checklist = dict()
	for key in keys:
		if xls_data[key] == dict[key]: checklist[key] = True
		else: checklist[key] = False
	finalchecklist.append(checklist)
#Final output
OUT = finalchecklist

EDIT: These codes are typed without checking.

1 Like