Skip to content

Conversation

@cclauss
Copy link
Contributor

@cclauss cclauss commented Jun 11, 2025

map() returned a list in Python 2 but returns an iterator in Python 3.

map() and filter() return iterators. If you really need a list and the input sequences are all of equal length, a quick fix is to wrap map() in list(), 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.

>>> planes = ('', 'a', 'ab', 'abc')
>>> map(len, planes)
<map object at 0x1031e74c0>
>>> [len(plane) for plane in planes]
[0, 1, 2, 3]

`map()` returned a list in Python 2 but returns a generator in Python 3.
```python
>>> planes = ('', 'a', 'ab', 'abc')
>>> map(len, planes)
<map object at 0x1031e74c0>
>>> [len(plane) for plane in planes]
[0, 1, 2, 3]
```
@matham matham merged commit 149409f into matham:master Jun 11, 2025
15 checks passed
@cclauss cclauss deleted the patch-1 branch June 11, 2025 16:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants