zip 2 sequences into 1

Discussion in 'Python' started by Neal Becker, Apr 22, 2004.

  1. Neal Becker

    Neal Becker Guest

    What's an easy/efficient way to zip together 2 (or more) sequences into a
    single sequence?

    I noticed zip builtin. This combines

    a_0 ... a_n
    b_0 ... b_n

    into

    (a_0 b_0)(a_1 b_1)...

    What I want is a single sequence

    a_0 b_0 a_1 b_1...
     
    Neal Becker, Apr 22, 2004
    #1
    1. Advertising

  2. Neal Becker wrote:
    > What's an easy/efficient way to zip together 2 (or more) sequences into a
    > single sequence?
    >

    How about

    itertools.chain(*zip(seq1, seq2, seq3, ...))

    For example:

    >>> a = 'abcde'
    >>> b = range(5)
    >>> c = 'python'
    >>> from itertools import chain
    >>> list(chain(*zip(a,b,c)))

    ['a', 0, 'p', 'b', 1, 'y', 'c', 2, 't', 'd', 3, 'h', 'e', 4, 'o']

    --
    George
     
    George Yoshida, Apr 22, 2004
    #2
    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. Alex Hunsley
    Replies:
    1
    Views:
    623
    Andrew Thompson
    Sep 16, 2004
  2. Replies:
    0
    Views:
    1,778
  3. HARI OM
    Replies:
    0
    Views:
    17,476
    HARI OM
    May 3, 2005
  4. Nickolay Kolev

    Zip with sequences of diffrent length

    Nickolay Kolev, Apr 26, 2004, in forum: Python
    Replies:
    2
    Views:
    301
    Peter Hansen
    Apr 26, 2004
  5. Bo Yang
    Replies:
    9
    Views:
    320
    -berlin.de
    Nov 20, 2006
Loading...

Share This Page