fixing map(None, [1,2,3]) ?

Mike Coleman mkc at mathdogs.com
Mon Mar 26 19:42:21 EST 2001


"Sean 'Shaleh' Perry" <shaleh at valinux.com> writes:
> On 26-Mar-2001 Mike Coleman wrote:
> > Shouldn't "map(None, [1,2,3])" return "[(1,), (2,), (3,)]"?
> > 
> > Is there any hope that this could be fixed?
> > 
> map runs the function on each element of the list it is passed and returns an
> element of a new list.  Why should it magically return a list of one-tuples?

The (documented) behavior of map when 'None' is given instead of a function is
special.  It would be very useful, too, except for the problem cited above:

   $ python
   Python 1.5.2 (#0, Apr  3 2000, 14:46:48)  [GCC 2.95.2 20000313 (Debian GNU/Linux)] on linux2
   Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
   >>> map(None, [1, 2, 3], [4, 5, 6])
   [(1, 4), (2, 5), (3, 6)]
   >>> map(None, [1, 2, 3], [4, 5, 6], [7, 8, 9])
   [(1, 4, 7), (2, 5, 8), (3, 6, 9)]
   >>> map(None, [1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12])
   [(1, 4, 7, 10), (2, 5, 8, 11), (3, 6, 9, 12)]

   >>> map(None, [1, 2, 3])
   [1, 2, 3]
   # shouldn't that be [(1,), (2,), (3,)]?

   >>> map(None) 
   Traceback (innermost last):
     File "<stdin>", line 1, in ?
   TypeError: map() requires at least two args
   # returning [] in this case would be nice for completeness

(Certainly I could be missing something here.)

--Mike

-- 
Mike Coleman, mkc at mathdogs.com
  http://www.mathdogs.com -- problem solving, expert software development




More information about the Python-list mailing list