zip 2 sequences into 1

George Yoshida ml at dynkin.com
Wed Apr 21 23:56:54 EDT 2004


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



More information about the Python-list mailing list