Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c9b644e

Browse files
committed
Merge fix for Issue #12666 from 3.2
2 parents 506b361 + a90e364 commit c9b644e

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

Doc/howto/pyporting.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,18 @@ Otherwise it might very well be worth your time and effort to port your tests
505505
to :mod:`unittest`.
506506

507507

508+
Update `map` for imbalanced input sequences
509+
'''''''''''''''''''''''''''''''''''''''''''
510+
511+
With Python 2, `map` would pad input sequences of unequal length with
512+
`None` values, returning a sequence as long as the longest input sequence.
513+
514+
With Python 3, if the input sequences to `map` are of unequal length, `map`
515+
will stop at the termination of the shortest of the sequences. For full
516+
compatibility with `map` from Python 2.x, also wrap the sequences in
517+
:func:`itertools.zip_longest`, e.g. ``map(func, *sequences)`` becomes
518+
``list(map(func, itertools.zip_longest(*sequences)))``.
519+
508520
Eliminate ``-3`` Warnings
509521
-------------------------
510522

Doc/whatsnew/3.0.rst

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,22 @@ Some well-known APIs no longer return lists:
154154
:meth:`dict.itervalues` methods are no longer supported.
155155

156156
* :func:`map` and :func:`filter` return iterators. If you really need
157-
a list, a quick fix is e.g. ``list(map(...))``, but a better fix is
157+
a list and the input sequences are all of equal length, a quick
158+
fix is to wrap :func:`map` in :func:`list`, e.g. ``list(map(...))``,
159+
but a better fix is
158160
often to use a list comprehension (especially when the original code
159161
uses :keyword:`lambda`), or rewriting the code so it doesn't need a
160162
list at all. Particularly tricky is :func:`map` invoked for the
161163
side effects of the function; the correct transformation is to use a
162164
regular :keyword:`for` loop (since creating a list would just be
163165
wasteful).
164166

167+
If the input sequences are not of equal length, :func:`map` will
168+
stop at the termination of the shortest of the sequences. For full
169+
compatibility with `map` from Python 2.x, also wrap the sequences in
170+
:func:`itertools.zip_longest`, e.g. ``map(func, *sequences)`` becomes
171+
``list(map(func, itertools.zip_longest(*sequences)))``.
172+
165173
* :func:`range` now behaves like :func:`xrange` used to behave, except
166174
it works with values of arbitrary size. The latter no longer
167175
exists.

0 commit comments

Comments
 (0)