Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

indexing an array

Tags:

python

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.

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

e.g:
["Andy","Alice","Bob","Beth","Charlie"] becomes
{"A":["Andy","Alice"],"B":["Bob","Beth"],"C":["Charlie"]}

My solution looks like this:

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

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

like image 1000
tylerl Avatar asked Jan 26 '26 12:01

tylerl


1 Answers

You can do the same a little simpler with a defaultdict:

from collections import defaultdict

def make_index(data,key,value=lambda x:x):
    d=defaultdict(list)
    for item in data:
        d[key(item)].append(value(item))
    return d

Using a defaultdict is faster than using .setdefault(), which would be the other option.

like image 98
Martijn Pieters Avatar answered Jan 28 '26 01:01

Martijn Pieters



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!