I was recently talking to a friend and he said that you can use Python's reduce() function for example to calculate the length of a list or even reverse a list. I tried to google the actual code, but I couldn't find any examples.
-
3Those are actually really bad uses of reduce, especially because getting the length and reversing a list is something that is available as a built-in already. Read this for some better examples: http://stackoverflow.com/questions/15995/useful-code-which-uses-reduce-in-python – idjaw Mar 13 '16 at 03:12
-
Those are some interesting examples, thank you for the link, but they don't talk about reversing a list or getting the length. Could you do that too? – TheTask1337 Mar 13 '16 at 03:16
-
2Why would you want to do that with reduce? If you want to get the length of a list, do `len(my_list)`. If you want to reverse a list, do `my_list.reverse()`. And searching your question on SO actually brings [this](http://stackoverflow.com/questions/33908847/reverse-list-using-map-reduce). Maybe that will help give you more information – idjaw Mar 13 '16 at 03:19
-
In other languages, `reduce` might be called `fold`. You may find examples using that term. – jscs Mar 13 '16 at 03:28
-
1You really shouldn't do it this way because there are better ways, but you can use `reduce(lambda x, y: x+1, mylist, 0)` to get the length and `reduce(lambda x, y: [y] + x, mylist, [])` to get the reversed list. – zondo Mar 13 '16 at 03:43
-
I think `[::-1]` is cooler because it works exactly the same for strings and lists. It doesn't reverse in place. `["a", "b"][::-1]` -> `['b', 'a']` and `"ab"[::-1]` -> `'ba'` – jDo Mar 13 '16 at 04:19
-
There is a nice example of cumsum using reduce here http://stackoverflow.com/questions/9258602/elegant-pythonic-cumsum – Erotemic Mar 13 '16 at 04:51
2 Answers
You really should use len()
and reversed()
, but if you want to use reduce()
, you can do it:
length = reduce(lambda x,y: x+1, mylist, 0)
reverse = reduce(lambda x,y: [y]+x, mylist, [])
For the length, we just need to keep track of a running total in our function. We therefore need to start out with a total of 0
. That can be passed to reduce()
as the initial
argument. Now, our function will first take the initial
and the first element in mylist
. It just ignores the second argument (the first element in the list) and returns x+1
. Right now x
is 0
, so it returns 1
. Next that and the second element in mylist
are given it. It returns the first argument, 1
plus 1
: 2
. It goes on like this until it reaches the end of mylist
at which point it has the total.
For the reversing, it is very similar. We start out with x
being []
. Then, we want to put the first element at the beginning, so we return [y] + x
. Next time it is called, it will be with [y] + x
and the second element in mylist
. It then puts that second element at the beginning and returns that, etc.

- 19,901
- 8
- 44
- 83
Reduce
can be a very powerful function in Python, along with map
and lambda
expressions. One example, is computing the sum of numbers from 1 to 100:
>>> reduce(lambda x, y: x+y, range(1,101))
5050
You can also use it to find the max of a list:
>>> f = lambda a,b: a if (a > b) else b
>>> reduce(f, [47,11,42,102,13])
102
You can even use it to flatten lists (this is have found to be very useful):
reduce(list.__add__, [[1, 2, 3], [4, 5], [6, 7, 8]], [])
[1, 2, 3, 4, 5, 6, 7, 8]

- 318
- 3
- 13
-
1You are aware that all of your three examples can be archived with the built-in functions `sum()` and `max()` in a far more simple way? – Klaus D. Mar 13 '16 at 08:52