zip 2 sequences into 1

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Neal Becker

    zip 2 sequences into 1

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



  • George Yoshida

    #2
    Re: zip 2 sequences into 1

    Neal Becker wrote:[color=blue]
    > What's an easy/efficient way to zip together 2 (or more) sequences into a
    > single sequence?
    >[/color]
    How about

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

    For example:
    [color=blue][color=green][color=darkred]
    >>> a = 'abcde'
    >>> b = range(5)
    >>> c = 'python'
    >>> from itertools import chain
    >>> list(chain(*zip (a,b,c)))[/color][/color][/color]
    ['a', 0, 'p', 'b', 1, 'y', 'c', 2, 't', 'd', 3, 'h', 'e', 4, 'o']

    --
    George

    Comment

    Working...