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: imap and map inconsistent behaviour
Type: behavior Stage: resolved
Components: Interpreter Core, Library (Lib) Versions: Python 2.7
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Ernesto Alfonso, ned.deily
Priority: normal Keywords:

Created on 2015-06-13 22:05 by Ernesto Alfonso, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (3)
msg245329 - (view) Author: Ernesto Alfonso (Ernesto Alfonso) Date: 2015-06-13 22:05
itertools.imap and map in Python 2.7 produces inconsistent behaviour when mapping the null (None) function over multiple sequences.

>>> [a for a in map(None, list("abcd"), range(3))]
[('a', 0), ('b', 1), ('c', 2), ('d', None)]
>>> from itertools import imap
>>> [a for a in imap(None, list("abcd"), range(3))]
[('a', 0), ('b', 1), ('c', 2)]
>>> [a for a in map(None, list("abcd"), range(3))] == [a for a in imap(None, list("abcd"), range(3))]

This inconsistent and unintuitive behvaiour caused a bug in my program
msg245331 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2015-06-14 00:43
This is a documented behavior difference in itertools.imap:

"If function is set to None, then imap() returns the arguments as a tuple. Like map() but stops when the shortest iterable is exhausted instead of filling in None for shorter iterables. The reason for the difference is that infinite iterator arguments are typically an error for map() (because the output is fully evaluated) but represent a common and useful way of supplying arguments to imap()."
msg245332 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2015-06-14 00:48
Also note that the behavior of map() in Python 3 has been changed to also stop with the termination of the shortest iterator.
Date User Action Args
2022-04-11 14:58:18adminsetgithub: 68634
2015-06-14 00:48:02ned.deilysetmessages: + msg245332
2015-06-14 00:43:40ned.deilysetstatus: open -> closed

nosy: + ned.deily
messages: + msg245331

resolution: not a bug
stage: resolved
2015-06-13 22:05:16Ernesto Alfonsocreate