# Map not working

**URL:** <https://discuss.python.org/t/map-not-working/41415>\
**Category:** Python Help\
**Created:** [December 18, 2023, 10:34pm UTC](https://discuss.python.org/t/map-not-working/41415 "2023-12-18T22:34:28Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnaweiss](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/johnaweiss/32/16560_2.png) [@johnaweiss](https://discuss.python.org/u/johnaweiss)\
**Post date:** [December 18, 2023, 10:34pm UTC](https://discuss.python.org/t/map-not-working/41415/1 "2023-12-18T22:34:28Z")

</div>

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

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

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

```

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [December 18, 2023, 10:38pm UTC](https://discuss.python.org/t/map-not-working/41415/2 "2023-12-18T22:38:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![johnaweiss](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/johnaweiss/32/16560_2.png) [@johnaweiss](https://discuss.python.org/u/johnaweiss)\
**Post date:** [December 18, 2023, 10:46pm UTC](https://discuss.python.org/t/map-not-working/41415/3 "2023-12-18T22:46:49Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![bschubert](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/bschubert/32/26920_2.png) [@bschubert](https://discuss.python.org/u/bschubert)\
**Post date:** [December 19, 2023, 12:30am UTC](https://discuss.python.org/t/map-not-working/41415/4 "2023-12-19T00:30:09Z")

</div>

> [@johnaweiss](#):
>
> I see my error: i used parens for the list, instead of brackets. It works with brackets.

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?](https://stackoverflow.com/questions/626759/whats-the-difference-between-lists-and-tuples)

> [@johnaweiss](#):
>
> Can you share the code to achieve this same thing using **list comprehensions**?

That would look something like this:

```python
>>> 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`:

```auto
>>> 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](https://docs.python.org/3.12/glossary.html#term-iterator) that lazily applies `myfunc` to each entry only as needed:

```auto
>>> 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:

- [List comprehension vs map](https://stackoverflow.com/questions/1247486/list-comprehension-vs-map)
- [Getting a map() to return a list in Python 3.x](https://stackoverflow.com/questions/1303347/getting-a-map-to-return-a-list-in-python-3-x)
- [Use map() for functions that does not return a value](https://stackoverflow.com/questions/47722712/use-map-for-functions-that-does-not-return-a-value)
- [Is it Pythonic to use list comprehensions for just side effects?](https://stackoverflow.com/questions/5753597/is-it-pythonic-to-use-list-comprehensions-for-just-side-effects)

---

<div class="post-metadata">

**Author:** ![johnaweiss](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/johnaweiss/32/16560_2.png) [@johnaweiss](https://discuss.python.org/u/johnaweiss)\
**Post date:** [December 19, 2023, 1:09am UTC](https://discuss.python.org/t/map-not-working/41415/6 "2023-12-19T01:09:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kknechtel](https://avatars.discourse-cdn.com/v4/letter/k/e47c2d/32.png) [@kknechtel](https://discuss.python.org/u/kknechtel)\
**Post date:** [December 19, 2023, 9:24am UTC](https://discuss.python.org/t/map-not-working/41415/7 "2023-12-19T09:24:41Z")

</div>

> [@johnaweiss](#):
>
> but the issue was simply use of parens instead of brackets.

That is absolutely not the case:

```auto
>>> # 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:

```auto
>>> 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:

```auto
>>> 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:

```auto
>>> 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]

```

---

<div class="post-metadata">

**Author:** ![johnaweiss](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/johnaweiss/32/16560_2.png) [@johnaweiss](https://discuss.python.org/u/johnaweiss)\
**Post date:** [December 19, 2023, 2:48pm UTC](https://discuss.python.org/t/map-not-working/41415/8 "2023-12-19T14:48:51Z")

</div>

Is there a bug in my initial post?

---

<div class="post-metadata">

**Author:** ![onePythonUser](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/onepythonuser/32/19158_2.png) [@onePythonUser](https://discuss.python.org/u/onePythonUser)\
**Post date:** [December 19, 2023, 3:47pm UTC](https://discuss.python.org/t/map-not-working/41415/9 "2023-12-19T15:47:06Z")

</div>

Hi,

there is no bug in your original post.

```auto
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:

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

```

Another potential solution, is to redefine your function:

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