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.

Title: container constructors destroy argument
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.0
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: kjwcode, rhettinger
Priority: normal Keywords:

Created on 2008-12-05 07:53 by kjwcode, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (2)
msg76975 - (view) Author: Kevin J. Woolley (kjwcode) Date: 2008-12-05 07:53
Doing the following (more info than necessary in case I'm doing
something weird):

def odd(n):
    return n % 2

x = (1, 2, 3, 4, 5)
y = filter(odd, x)

Will correctly build a list from y and return [1, 3, 5] on the first
call to list(), but will return an empty list thereafter.  y still
identifies itself as a filter object, but you either can't coerce it to
anything else or the value of the filter object is reset (I don't know
how to tell the difference yet).

The other container constructors (set, tuple, etc.) seem to have the
same behaviour.
msg76977 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2008-12-05 08:00
In Py3.0, the filter() builtin returns a consumable iterator, not a list.
It's a feature, not a bug ;-)
For the behavior you want, write:
   y = list(filter(odd, x))
Or better yet, use a list comprehension:
   y = [e for e in x if odd(e)]
See the Whatsnew section of the docs for further explanation.
Date User Action Args
2022-04-11 14:56:42adminsetgithub: 48793
2008-12-05 08:00:17rhettingersetstatus: open -> closed
resolution: not a bug
messages: + msg76977
nosy: + rhettinger
2008-12-05 07:53:40kjwcodecreate