This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Iteration over a map object with list()
Type: Stage:
Components: None Versions: Python 3.0
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: jmfauth, rhettinger, terry.reedy
Priority: normal Keywords:

Created on 2008-11-17 15:54 by jmfauth, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
unnamed jmfauth, 2008-11-18 07:35
Messages (4)
msg75965 - (view) Author: jmf (jmfauth) Date: 2008-11-17 15:54
win XP sp2, Py3.0c2

I had to face an annoying problem when iterating over a map object.

With a range class, this works

>>> r = range(5)
>>> list(r)
[0, 1, 2, 3, 4]

With dict_keys/values/items objects, the following works

>>> d = {1: 'a', 2:'b', 3:'c'}
>>> list(d.keys())
[1, 2, 3]
>>> list(d.values())
['a', 'b', 'c']
>>> list(d.items())
[(1, 'a'), (2, 'b'), (3, 'c')]

But with a map object...

>>> def plus(i):
        return i + 1

>>> a = list(range(3))
>>> a
[0, 1, 2]
>>> r = map(plus, a)
>>> r
<map object at 0x01371570>
>>> for e in r: print(e)

1
2
3
>>> list(r)
[]
>>> #empty list!

Bug or feature?
msg75969 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2008-11-17 16:06
Feature :-)

You will get the expected result if you skip the step where you ran the
for-loop over r before running list().  Either listing or for-looping
will exhaust the iterator.  This is how iterators work.
msg76001 - (view) Author: jmf (jmfauth) Date: 2008-11-18 07:35
2008/11/17 Raymond Hettinger <report@bugs.python.org>

>
> Raymond Hettinger <rhettinger@users.sourceforge.net> added the comment:
>
> Feature :-)
>
> You will get the expected result if you skip the step where you ran the
> for-loop over r before running list().  Either listing or for-looping
> will exhaust the iterator.  This is how iterators work.
>
> ----------
> nosy: +rhettinger
> resolution:  -> invalid
> status: open -> closed
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue4337>
> _______________________________________
>

Thanks for the reply and sorry for the noise. Indeed, you are right and for
some "strange personal reasons" (bad habits?), I frequently fall in this
trap.

    return i + 1

[1, 2, 3, 4]
>>>

Regards
msg76229 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2008-11-22 01:25
Dict views and range objects are *iterables* because they are based on
reusable information.  Map, filter, and similar objects are *iterators*
because they are based on iterables that could be once-through
iterators. The built-in function entries carefully specific which is which.
History
Date User Action Args
2022-04-11 14:56:41adminsetgithub: 48587
2008-11-22 01:25:34terry.reedysetnosy: + terry.reedy
messages: + msg76229
2008-11-18 07:35:23jmfauthsetfiles: + unnamed
messages: + msg76001
2008-11-17 16:06:04rhettingersetstatus: open -> closed
resolution: not a bug
messages: + msg75969
nosy: + rhettinger
2008-11-17 15:54:35jmfauthcreate