Velocity Reviews > Zip with sequences of diffrent length

# Zip with sequences of diffrent length

Nickolay Kolev
Guest
Posts: n/a

 04-26-2004
Hi all,

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?

Nicky

Mike C. Fletcher
Guest
Posts: n/a

 04-26-2004
Use map( None, l, l[1:] )

HTH,
Mike

Nickolay Kolev wrote:
....

> 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?

....
_______________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://members.rogers.com/mcfletch/

Peter Hansen
Guest
Posts: n/a

 04-26-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)]