DEV Community

Discussion on: Using fetch() and reduce() to grab and format data from an external API - A practical guide

Collapse
 
thor77 profile image
Thor77

Thanks for this great article.
I had to try Python's itertools.groupby because of this comment to filter the results:

data = requests.get(...).json()
agency_keyfunc = operator.itemgetter('agency')
agency_frequency = dict(
    map(
        lambda gt: (gt[0], len(list(gt[1]))),
        itertools.groupby(
            sorted(data, key=agency_keyfunc),
            agency_keyfunc
        )
    )
)
Enter fullscreen mode Exit fullscreen mode