can list comprehensions replace map?

Larry Bates lbates at syscononline.com
Wed Jul 27 14:43:38 EDT 2005


This isn't really a question about list
comprehensions as you are using a "feature"
of map by passing None as the function to be
executed over each list element:

This works when len(x) > len(y):

zip(x,y+(len(x)-len(y))*[None])

This works when len(y) >=0 len(x):

zip(x+(len(x)-len(y))*[None],y)

I would probably wrap into function:

def foo(x,y):
    if len(x) > len(y):
        return zip(x,y+(len(x)-len(y))*[None])

    return zip(x+(len(x)-len(y))*[None],y)

Larry Bates

David Isaac wrote:
> Newbie question:
> 
> 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'?)
> 
> Thanks,
> Alan Isaac
> 
> 



More information about the Python-list mailing list