can list comprehensions replace map?

Paolino paolo.veronelli at gmail.com
Thu Jul 28 02:11:42 EDT 2005


Raymond Hettinger wrote:
> [David Isaac]
> 
>>>I have been generally open to the proposal that list comprehensions
>>>should replace 'map', but I ran into a need for something like
>>>map(None,x,y)
>>>when len(x)>len(y).  I cannot it seems use 'zip' because I'll lose
>>>info from x.  How do I do this as a list comprehension? (Or,
>>>more generally, what is the best way to do this without 'map'?)
> 
> 
> [Paolino]
> 
>>Probably zip should change behaviour,and cover that case or at least
>>have another like 'tzip' in the __builtins__ .Dunno, I always thought
>>zip should not cut to the shortest list.
> 
> 
> Heck no!  For the core use case of lockstep iteration, it is almost
> always a mistake to continue iterating beyond the length of the
> shortest input sequence.  Even for map(), the use cases are thin.  How
> many functions do something meaningful when one or more of their inputs
> changes type and becomes a stream of Nones.  Consider for example,
> map(pow, seqa, seqb) -- what good can come of one sequence or the other
> suddenly switching to a None mode?
> 
> As Andrew pointed out, if you really need that behavior, it can be
> provided explicity.  See the padNone() recipe in the itertools
> documentation for an easy one-liner.
> 
> IMO, reliance on map's None fill-in feature should be taken as a code
> smell indicating a design flaw (not always, but usually).  There is a
> reason that feature is missing from map() implementations in some other
> languages.
> 
> In contrast, the existing behavior of zip() is quite useful.  It allows
> some of the input sequences to be infinite:
> 
>    zip(itertools.count(1), open('myfile.txt'))
> 
Right point.
Well, for my little experiences use cases in which the lists have different
lengths are rare, but in those cases I don't see the reason of not being 
able
to zip to the longest one.What is really strange is that I have to use
map(None,....) for that,instead of another zip-like function which ,at 
least
would be intutitive for the average user.Also map(None,...) looks like a 
super-hack
and it's not elegant or readable or logic (IMO)

I think zip comes to substitute the tuple.__new__ untolerant 
implementation.A dumb like me wuold expect map(tuple,[1,2,3],[2,3,4]) to 
work, so pretending map(None,....) would do it is like saying that None 
and tuple are near concepts, which is obviously an absurdity.

Thanks anyway, for explanations.

Paolino

> 
> Raymond
> 



More information about the Python-list mailing list