def combine_dicts(a, b): if b is None: return a return dict(a.items() + b.items() + [(k, combine_dicts(a[k], b[k])) for k in set(b) & set(a)])
a and b should both be dict type data. How to understand this function, especially the last return? ?
This is how it is written in Python 2. Come to Python version 3.6:
It should be more efficient. Everything else is pretty much the same.
It’s not a god-level code, nor is it difficult to understand. Just recursively merge values with the same key. What you need to know:
dict’s items method
tuples
Gathering together
The meaning of dict parameters
Ask a question, is there something wrong with the code? If the value in the same key is a string, will the items function report an error?