using defaultdict in a array in python 2.7
<p>I'm trying to create a dictionary where the same key can have different values from an array. In the past i created a dictionary with positions on each list of the list, but I found a bug where the same key can have more than one value and it's breaking the main code.
ie.</p>

<pre><code>my_array = [['a','b','c'],['a','e','d'],['f','g','h']]
my dict = dict([p[0], p[2]] for p in my_array)
my dict = {'a': 'c', 'f': 'h'}
What I need is:
my_dict = {'a': ['c','d'], 'f': 'h'}
</code></pre>

<p>I have tried with defaultdict but so far I'm getting TypeError Exception. I have tried initializing it as a string defaultdict(str) and a list defaultdict(list) with no success</p>

<pre><code>my_dict = defaultdict((my_dict[p[0]].append(p[2])) for p in my_array)
or
my_dict = defaultdict(([p[0], p[2]]).append(p[2]) for p in a_array)
</code></pre>

<p>Always getting TypeError exception, suggestions please?</p>

<p>You can't do this with a comprehension/generator expression. Instead, try this:</p>

<pre><code>my_dict = {}
for p in my_array:
    my_dict.setdefault(p[0], []).append(p[2])
</code></pre>

<p>You can also do it with a <code>defaultdict</code> if you insist, but it seems like overkill:</p>

<pre><code>my_dict = collections.defaultdict(list)
for p in my_array:
    my_dict[p[0]].append(p[2])
</code></pre>

<p>All your values will now be lists, even if they contain only a single item, which doesn't appear to be what you want based on your question. It probably actually is, as it's much easier to deal with values that are always the same type. But if you do want the single values outside of lists, I think the most straightforward way is to post-process after building the dictionary and convert single-item lists back to single values:</p>

<pre><code>for k, v in my_dict.iteritems():  # n.b. just .items() in Python 3
    if len(v) == 1:
        my_dict[k] = v[0]
</code></pre>

<p>Otherwise, you end up having to check the type of each value already in the dictionary when you're adding a new one, and convert the value to a list when you go to add the second one.</p>

python python-2.7