Using the map function

dictionary, python

Solution

The problem is that a list is not created. `map` returns a specific type of iterator in Python 3 that is not a list (but rather a 'map object', as you can see). You can try

print(list(squares))

Or just use a list comprehension to obtain a list in the first place (which seems to work better here anyway):

squares = [x**2 for x in range(10)]

`map` used to return a list in Python 2.x, and the change that was made in Python 3 is described in this section of the documentation:

- `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).

Problem

I'm having trouble with the `map` function. When I want to print the created list, the interpreter shows the pointer: ``` >>> squares = map(lambda x: x**2, range(10)) >>> print(squares) <map object at 0x0000000002A086A0> ``` What is the problem?

Original source