Python 2 vs Python 3 - Difference in map behavior with three arguments?


The following code behaves differently in Python 2 vs Python 3:

all(map(lambda x,y: x, [1, 2], [1, 2, 3]))

Python 2 gives False whereas Python 3 gives True. The documentation for Python 2 says that it will supply None if the shorter list is exhausted but Python 3 doesn't do that.

I am working on a code that really needs the length to be maintained for some reason. What is the cleanest way to get the old behavior? I know I can use from past.builtin import map as old_map, but is there a more elegant solution that would work in both versions?


Essentially, map with multiple iterables for the arguments will zip the iterables, and then call the function with the tuples from the zip as var-args. So, you can get the same behaviour using itertools.starmap and zip:

<pre class="lang-py prettyprint-override">>>> a = [10, 20] >>> b = [1, 2, 3] >>> f = lambda x, y: x >>> list(map(f, a, b)) [10, 20] >>> from itertools import starmap >>> list(starmap(f, zip(a, b))) [10, 20]

Then the behaviour you want can be achieved by replacing zip with itertools.zip_longest:

<pre class="lang-py prettyprint-override">>>> from itertools import starmap, zip_longest >>> list(starmap(f, zip_longest(a, b))) [10, 20, None]

Both functions from itertools also exist in Python 2, except the second one is named izip_longest instead. You can just import ... as ... to get around that.



  • ImagePullBackOff on GKE with Private Google Cloud Repository
  • Excel Macro send email
  • Prevent Right Floated Elements from Swapping
  • C++ Builder vs Delphi vs MFC
  • Azure Functions timeout for Consumption plan
  • Documenting Macro Functions in C++ with Doxygen
  • TabControl caching with code behind layout changes
  • put a value into hashmap which uses wildcard in java can not assign value
  • having a condition to fragment adding
  • Sphinx4 ConfidenceResult and SpeechResult
  • Recognize Patterns of images JPG or PNG
  • Texture streaming in DirectX11, Immutable vs Dynamic
  • Migrating MOSS 2007 from SQL 2000 to SQL 2005 [closed]
  • Newtonsoft.json serializing and deserializing base/inheirited where classes are from shared projects
  • Getting specific color for specific value using JSON data
  • Key Vault Settings in Azure App Settings with no code
  • Finding all XML nodes between each two processing instructions
  • Use 2D Text into 3D scenes in JavaFX results in blurry texts
  • MayAVI install on Python 3.6 [duplicate]
  • JQuery Mobile Ajax Navigation in Single-Page Template
  • How to add html image in to velocity template file to send email?
  • Create an Office365 mailbox from within C# Web API method
  • Creating 2d platforms using JavaScript
  • How to merge objects within array based on attribute
  • How to write seo friendly url's using htaccess?
  • concise way of flattening multiindex columns
  • Is there a better way for handling SpatialPolygons that cross the antimeridian (date line)?
  • Google Spreadsheet Script to Blink a range of Cells
  • Problems to understand DXGI DirectX 11 Desktop Duplication to get a Buffer or Array
  • I am consuming a WCF service that requires headers from a .NET 2 website. How can I programmatically
  • How do I add a mouse over tooltip to an Image using .DrawImage()
  • Google App Engine Datastore: Dealing with eventual consistency
  • Debug `Unexpected end of JSON input Error` on content script
  • Unity3d lost directional light shadows after generate assetBundle (.unity3d file)
  • Bitrate JWplayer
  • How to use FirstOrDefault inside Include
  • Firebase: How to read from external DB?
  • How to mutate multiple variables without repeating codes?
  • Time Complexity of Fibonacci Algorithm [duplicate]
  • How to check if object is null in Java?