Consolidate python dictionary list
<p>Original python dictionary list:</p>

<pre><code>    [
     {"keyword": "nike", "country":"usa"},
     {"keyword": "nike", "country":"can"},
     {"keyword": "newBalance", "country":"usa"},
     {"keyword": "newBalance", "country":"can"}
    ]
</code></pre>

<p>I would like to consolidate the python dict list and get an output like:</p>

<pre><code>    [
     {"keyword": "nike", "country":["usa","can"]},
     {"keyword": "newBalance", "country":["usa","can"]}
    ]
</code></pre>

<p>What is the most efficient way to do this?</p>

<p>Here are some general pointers. I'm not going to code the whole thing for you. If you post more of what you've tried and specific code you need help with, I'd be happy to help more.</p>

<p>It looks like you're combining only those which have the same value for the "keyword" key. So loop over values of that key and combine them based on that.</p>

<p>To combine a bunch of dictionaries once you've split them up as above, you'll first need to create a new one where "country" maps to an empty list. Then as you consider each of the dictionaries, check if its value for "country" is already in that list. If it's not, <code>append</code> it.</p>

python python-2.7