Using reduce with initial value in a Pipe with toolz Python

318 Views Asked by At

I am looking to perform a reduce operation as part of a pipe to sort dict items based on a key. e.g.

from toolz import pipe

items = [{"id":1, "val":1}, {"id":2, "val":2}, {"id":2, "val":3}]
res = pipe(items, reduce(combine_items), other_ops...)
# res = {1: [{'id': 1, 'val': 1}], 2: [{'id': 2, 'val': 2}, {'id': 2, 'val': 3}]}

def combine_items(record_list, item):
    record_list.setdefault(item.get("id"), []).append(item)
    return record_list

I've managed to do this using a lambda i.e.

res = pipe(items, lambda x: reduce(combine_items, x, {}), other_ops...)

My question is whether there is another way I can do this without having to use a lambda within the pipe?

The main issue I have is that I need a default value for my list append to work properly within the reduce and I am unsure if there is a way to fill this correctly within the pipe otherwise.

1

There are 1 best solutions below

1
filbranden On BEST ANSWER

Rather than rolling your own combine_items, you can use groupby() from toolz which does exactly what you're trying to do here:

from toolz import groupby

res = groupby('id', items)

If you want to use that as part of a pipe, then you can use functools.partial() to pass groupby() a key argument:

from toolz import groupby, pipe
from functools import partial

res = pipe(items, partial(groupby, 'id'), other_ops...)