indexing an array
<p>I find myself frequently making indexed lists from flat ones in Python. This is such a common task that I was wondering if there's a standard utility that I should be using for it.</p>

<p>The context is this: given an array, I need to create a dict of smaller arrays using some key for grouping.</p>

<p>e.g:<br>
<code>["Andy","Alice","Bob","Beth","Charlie"]</code> becomes<br>
<code>{"A":["Andy","Alice"],"B":["Bob","Beth"],"C":["Charlie"]}</code></p>

<p>My solution looks like this:</p>

<pre><code>def make_index(data,key,value=lambda x:x):
    d={}
    for item in data:
        k = key(item)
        v = value(item)
        try: d[k].append(v)
        except KeyError: d[k]=[v]
    return d
</code></pre>

<p>It's simple and all, but am I reinventing something that is implemented better elsewhere?</p>
