How to use filter, map, and reduce in Python 3

filter, functional-programming, python, python-3.x, reduce

Solution

You can read about the changes in What's New In Python 3.0. You should read it thoroughly when you move from 2.x to 3.x since a lot has been changed.

The whole answer here are quotes from the documentation.

Views And Iterators Instead Of Lists

Some well-known APIs no longer return lists:

- [...]

- `map()` and `filter()` return iterators. If you really need a list, a quick fix is e.g. `list(map(...))`, but a better fix is often to use a list comprehension (especially when the original code uses lambda), or rewriting the code so it doesn’t need a list at all. Particularly tricky is `map()` invoked for the side effects of the function; the correct transformation is to use a regular `for` loop (since creating a list would just be wasteful).

- [...]

Builtins

- [...]

- Removed `reduce()`. Use `functools.reduce()` if you really need it; however, 99 percent of the time an explicit `for` loop is more readable.

- [...]

Problem

This is how I am accustomed to `filter`, `map`, and `reduce` working in Python 2: ``` >>> def f(x): return x % 2 != 0 and x % 3 != 0 >>> filter(f, range(2, 25)) [5, 7, 11, 13, 17, 19, 23] >>> def cube(x): return x*x*x >>> map(cube, range(1, 11)) [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000] >>> def add(x,y): return x+y >>> reduce(add, range(1, 11)) 55 ``` However, all of these seem to break in Python 3: ``` >>> filter(f, range(2, 25)) <filter object at 0x0000000002C14908> >>> map(cube, range(1, 11)) <map object at 0x0000000002C82B70> >>> reduce(add, range(1, 11)) Traceback (most recent call last): File "<pyshell#8>", line 1, in <module> reduce(add, range(1, 11)) NameError: name 'reduce' is not defined ``` Why are the results different? How can I get Python 3 code to work like the Python 2 code did? See also: What is the problem with reduce()? for specific motivation for the change to put `reduce` into a standard library module rather than leaving it as a builtin. See Getting a map() to return a list in Python 3.x for more specific answers about `map`.

Original source

Related problems