Skip to content
Advertisement

How to merge two nested dictionaries in python, which have same keys and have array type values?

I need to merge two dictionaries d1 and d2, where:

JavaScript

I need dictionary d3 after merging d1 and d2, such that, 1st value of key:0 of d1 is merged with 1st value of key:0 of d2.

Expecting d3 as:

JavaScript

I have tried multiple approaches, but nothing seems to work appropriately. Any help is much appreciated! Thanks!

Advertisement

Answer

I hope I’ve understood your question well. You can use itertools.zip_longest to alternate between the values from the two dictionaries:

JavaScript

Prints:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement