Map not working

I’m only getting the final “done” output. What’s my mistake? Does map require an include?

def myfunc(n):
    print("n: ", n)
    return len(n)


x = map(myfunc, ('apple', 'banana', 'cherry'))
print("done")
1 Like

The map object itself is a lazy iterable. If you iterate over it - such as converting it to a list - it will do the actual work of mapping your function over your iterables.

You might want to look into list comprehensions as an alternative to map() - both have value, take your pick.

I see my error: i used parens for the list, instead of brackets. It works with brackets.
Can you share the code to achieve this same thing using list comprehensions?
Thx!

Do you mean ('apple', 'banana', 'cherry') vs ['apple', 'banana', 'cherry']? If so, both work just as well in this case. The former creates a tuple while the latter creates a list. Both are sequences that map can happily iterate over. See also What’s the difference between lists and tuples?

That would look something like this:

>>> x = [myfunc(fruit) for fruit in ('apple', 'banana', 'cherry')]
n:  apple
n:  banana
n:  cherry
>>> print(x)
[5, 6, 6]

As you can see, this results in myfunc being called immediately for each of the fruits. You can get the same behavior using map if you pass the iterator that it returns to list:

>>> x = list(map(myfunc, ('apple', 'banana', 'cherry')))
n:  apple
n:  banana
n:  cherry
>>> print(x)
[5, 6, 6]

Calling map on its own returns an iterator that lazily applies myfunc to each entry only as needed:

>>> x = map(myfunc, ('apple', 'banana', 'cherry'))
>>> next(x)
n:  apple
5
>>> next(x)
n:  banana
6
>>> next(x)
n:  cherry
6
>>> next(x)
Traceback (most recent call last)
... 
StopIteration

For more on list compressions vs map, see the following SO questions:

2 Likes

Thx for your long answer, but the issue was simply use of parens instead of brackets.

That is absolutely not the case:

>>> # no output either way, because `myfunc` was not called yet
>>> x = map(myfunc, ('apple', 'banana', 'cherry'))
>>> x = map(myfunc, ['apple', 'banana', 'cherry'])
>>> 

Correct solutions similarly do not care whether the input for the map is a list (using brackets) or a tuple (using parens). Converting to a list:

>>> lengths = list(map(myfunc, ('apple', 'banana', 'cherry')))
n:  apple
n:  banana
n:  cherry
>>> lengths
[5, 6, 6]
>>> lengths = list(map(myfunc, ['apple', 'banana', 'cherry']))
n:  apple
n:  banana
n:  cherry
>>> lengths
[5, 6, 6]

Using an explicit loop:

>>> for length in map(myfunc, ('apple', 'banana', 'cherry')):
...     print(length)
... 
n:  apple
5
n:  banana
6
n:  cherry
6
>>> for length in map(myfunc, ['apple', 'banana', 'cherry']):
...     print(length)
... 
n:  apple
5
n:  banana
6
n:  cherry
6

List comprehensions don’t care, either:

>>> lengths = [myfunc(fruit) for fruit in ('apple', 'banana', 'cherry')]
n:  apple
n:  banana
n:  cherry
>>> lengths
[5, 6, 6]
>>> lengths = [myfunc(fruit) for fruit in ['apple', 'banana', 'cherry']]
n:  apple
n:  banana
n:  cherry
>>> lengths
[5, 6, 6]
1 Like

Is there a bug in my initial post?

Hi,

there is no bug in your original post.

x = map(myfunc, ('apple', 'banana', 'cherry'))  # This creates an 'object'

>>> x
<map object at 0x0000019302E96860>

What was missing was either the ‘list’ or ‘tuple’ keyword to unpack it
(I suppose you can call that a ‘bug’).

So, had you defined it as (which others have already highlighted in other posts) as shown
below, there would have been no issue and you would have gotten your expected result:

x = list(map(myfunc, ('apple', 'banana', 'cherry')))
or
x = tuple(map(myfunc, ('apple', 'banana', 'cherry')))

Another potential solution, is to redefine your function:

def myfunc(n):
    print('n: {}, {}'.format(len(n), n))

>>> d =  list(map(myfunc, ('apple', 'banana', 'cherry'))) 

n: 5, apple
n: 6, banana
n: 6, cherry

I am making the assumption that you only wanted the values printed and that you did not need
the values for some additional processing. If you do, then add the return built-in statement as in
your original post. One thing to note is that a function without a return statement will return a
None statement by default. So, if you print the ‘d’ as is, you will get three Nones in a list. It is only
after including the ‘return’ statement explicitly in the function, that the default ‘None’ statements
will be overwritten.