Search code examples
pythonpython-3.xreducefunctools

Python reduce in list of dictionaries


I am a Java developer; however, occasionally due to cold starts in aws lambda for Java, I decide to use python for simple lambdas since it doesn't require compilation, eliminating cold starts.

The problem: I have a list of dictionaries, I retrieve this from Dynamo, in short a valid example is the following

[
   {
    "data":"data",
    "Count": 60
   },
   {
    "data":"data",
    "Count": 60
   }
]

I am trying to reduce this by getting the sum of the Count fields by doing the following; however, I don't understand what I am missing.

reduce(lambda x, y: int(x['Count']) +
                  int(y['Count']), query_response)

This fails with "errorMessage": "'int' object is not iterable"

What is the correct approach to doing this ?

Thanks in advance.


Solution

  • You need to give x an initial value of 0:

    >>> from functools import reduce
    >>> query_reponse = [
    ...    {
    ...     "data": "data",
    ...     "Count": 60
    ...    },
    ...    {
    ...     "data": "data",
    ...     "Count": 60
    ...    }
    ... ]
    >>> reduce(lambda x, y: x + y["Count"], query_reponse, 0)
    120
    

    Ideally, you would want to use more descriptive variable names, e.g.:

    >>> reduce(lambda total, d: total + d["Count"], query_reponse, 0)
    120
    

    For more info look at the docs for functools.reduce

    As the other answer suggests you probably want to use sum() for small tasks like this. However, I do still think learning how reduce works is important with its prevalence in things like PySpark/Spark or other languages.