468,284 Members | 1,551 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,284 developers. It's quick & easy.

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

Jul 18 '05 #1
1 1191
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
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by temp | last post: by
10 posts views Thread by Vilson farias | last post: by
4 posts views Thread by Hemant Shah | last post: by
1 post views Thread by J.Haan | last post: by
2 posts views Thread by Peteris Krumins | last post: by
3 posts views Thread by Ken | last post: by
4 posts views Thread by JJ | last post: by
2 posts views Thread by MrBee | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.