Skip to content
Advertisement

how to combine (and add) some value of list of dict in Python 3?

I have two list of dict :

JavaScript

I want a dict (or list of dict) as :

JavaScript

Can you help me ?

Advertisement

Answer

To do this efficiently, use a defaultdict and iterate over each nested entry.

JavaScript

Result:

JavaScript

Note: it looks like {'Coin': 'DDD', 'Price': '4'} is excluded from your result and is unclear why. If that is intentional and you want something like only entries that are members of both lists, you would want to keep a counter and weed out those entries that do not have a total count equal to the number of lists.

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