How to Recursively Reduce a List Down to a Single Value

How to Recursively Reduce a List Down to a Single Value

Normally when you map a list of values to a function you get back the same number of values you started with. The function is evaluated for each member of your list. When you want to reduce a list down to one we have the reduce function. As it turns out there is a reduce function in Python and R and it appears to work pretty similarly.

The way both implementations work is to recursively apply the function to successive pairs in the list. The result from each iteration is passed through to the next item and evaluated. Only the final result of these iterations is returned by the reduce functions.

Using the reduce function to return a single value from a list

As you can see the list of jedi names is returned as as single string. This pattern can be applied to numbers and more complicated lists as well.

Gezahagn Negash Seboka

Research Team Leader & GIS Analyst

3 年

Thanks for sharing!

Warwick Smith

Operational Excellence | Decision Automation | AI | Digital Twins

3 年

Data science interpretation on how multiple jedi become one with the force?

要查看或添加评论,请登录

Matt Rosinski的更多文章

社区洞察

其他会员也浏览了