VBA and Python – Intersection of dictionaries

Intersection of dictionary (or of any two lists) is a list with elements, that are present in both other lists.

A random tree.

Thus, if we have the following 3 dictionaries in Python:

Then, an intersection of their keys will contain only aa and bb , as these are the only keys, present in each one of them. With Python, making an intersection, union or a difference is actually quite easy. You need only 1 single binary operator and it works:

However, with VBA (I bet 10 beers, this is the only article in the whole net, comparing VBA with Python, based on dictionary intersection) the story is not that charming. You will probably need a whole function for this. I have built a function, returning an empty dictionary, with keys that are intersection of the keys of 2 input dictionaries:

That’s all, folks!

Tagged with: , , , , , ,