Zip with sequences of diffrent length

Peter Hansen peter at engcorp.com
Mon Apr 26 13:21:54 EDT 2004


Nickolay Kolev wrote:

> I want to make tuples of the elements of a list like this.
> 
> l = [1, 2, 3]
> 
> Wanted tuples:
> 
> (1, 2)
> (2, 3)
> (3, None)
> 
> I can get the first two tuples using zip(l, l[1:]). How do I get the 
> last one?

 >>> l = [1, 2, 3]
 >>> zip(l, l[1:]+[None])
[(1, 2), (2, 3), (3, None)]








More information about the Python-list mailing list