I have
a = [1, 2]
b = ['a', 'b']
I want
c = [1, 'a', 2, 'b']
An alternate method using index slicing which turns out to be faster and scales better than zip:
def slicezip(a, b):
result = [0]*(len(a)+len(b))
result[::2] = a
result[1::2] = b
return result
You'll notice that this only works if len(a) == len(b)
but putting conditions to emulate zip will not scale with a or b.
For comparison:
a = range(100)
b = range(100)
%timeit [j for i in zip(a,b) for j in i]
100000 loops, best of 3: 15.4 µs per loop
%timeit list(chain(*zip(a,b)))
100000 loops, best of 3: 11.9 µs per loop
%timeit slicezip(a,b)
100000 loops, best of 3: 2.76 µs per loop
zip(a,b)
gives you the answer). - jfs