Zip with sequences of diffrent length

Discussion in 'Python' started by Nickolay Kolev, Apr 26, 2004.

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

    Many thanks in advance,
    Nicky
     
    Nickolay Kolev, Apr 26, 2004
    #1
    1. Advertising

  2. 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/
     
    Mike C. Fletcher, Apr 26, 2004
    #2
    1. Advertising

  3. Nickolay Kolev

    Peter Hansen Guest

    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)]
     
    Peter Hansen, Apr 26, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Abs
    Replies:
    0
    Views:
    522
  2. Abs
    Replies:
    0
    Views:
    410
  3. Neal Becker

    zip 2 sequences into 1

    Neal Becker, Apr 22, 2004, in forum: Python
    Replies:
    1
    Views:
    280
    George Yoshida
    Apr 22, 2004
  4. Anthra Norell

    unpaking sequences of unknown length

    Anthra Norell, Aug 27, 2006, in forum: Python
    Replies:
    4
    Views:
    322
    Anthra Norell
    Aug 28, 2006
  5. Tom Best
    Replies:
    6
    Views:
    369
    Robert Dober
    Aug 10, 2009
Loading...

Share This Page