Python Forum

Full Version: pop default of two list of dictionary
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi team,

I have two list of dictionary, we need below output format. kindly help us.

list1 = [{'type': 'cumulative', 'domain': 'NEED', 'success': 26, 'failure': 7}]
list2 =  [{'type': 'environment', 'environment': 'ACP', 'domain': 'NEED', 'total_hits': 11, 'success': 11, 'failure': 0}, {'type': 'environment', 'environment': 'DIT', 'domain': 'NEED', 'total_hits': 11, 'success': 4, 'failure': 7}, {'type': 'environment', 'environment': 'INT', 'domain': 'NEED', 'total_hits': 11, 'success': 11, 'failure': 0},
{'type': 'environment', 'environment': 'ACP', 'domain': 'FEED', 'total_hits': 11, 'success': 11, 'failure': 0}, {'type': 'environment', 'environment': 'DIT', 'domain': 'FEED', 'total_hits': 11, 'success': 4, 'failure': 7}, {'type': 'environment', 'environment': 'INT', 'domain': 'FEED', 'total_hits': 11, 'success': 11, 'failure': 0}]
O/P Required :
Pop default should be "domain" and "type
Output:
{ NEED: { cumulative :[{ 'success': 26, 'failure': 7 }], environment : [ ACP : {'total_hits': 11, 'success': 11, 'failure': 0 }, DIT : {'total_hits': 11, 'success': 4, 'failure': 7 }, INT : {'total_hits': 11, 'success': 11, 'failure': 0 } ] }, FEED: { environment : [ ACP : {'total_hits': 11, 'success': 11, 'failure': 0 }, DIT : {'total_hits': 11, 'success': 4, 'failure': 7 }, INT : {'total_hits': 11, 'success': 11, 'failure': 0 } ] } }
The syntax of your output is not valid Python, and your question is so vague and odd (pop doesn't have a default) that I'm not sure what even looking for. My guess is that you would want to start with this dictionary:

data = {'NEED': {'cumulative': [], 'environment': {}}, 'FEED': {'cumulative': [], 'environment': {}}}
The you would loop through your lists and use conditionals to figure out which slot to add each dictionary in the list to, and whether it would be added with an append or an assignment to a dictionary key.