Handle duplicate keys in python dictionary
<p>Sorry in advance if this question has been answered before but I can't seem to find it.</p>

<p>I have panda dataframe like so:</p>

<pre><code>id | value1 | value2 | ... | valueN
1  | 321    | 44     | ... | 7766
2  | 5678   | 7638   | ... | 987423
2  | 0971   | 7638   | ... | 1
and so on...
</code></pre>

<p>I load it correctly and what I want to achieve is an <code>OrderedDict</code> which will collapse the double values if needed. For the above example,</p>

<p>the output dictionary should be:</p>

<pre><code>{1: ['321', '44', ..., '7766'], 2:['5678,0971', '7638', ..., '987423,1']}
</code></pre>

<p>Notice that the values of the dictionary are <code>list</code> and the values of the list are <code>strings</code>.</p>

<p>My code so far is:</p>

<pre><code>od = collections.OrderedDict()
for k in df.id:
        if k in od:
            # This key, pre-exists in this dictionary, so we have to append values
            # what should I do here?
        else:
            # new value inserted. proceed.
            od[k] = unordered_dict.get(k)
</code></pre>

<p>any ideas?</p>