Q:

Best way to merge two or more list in python preserving order

Is there a more pythonic way to do it ?
Input:
list1 =  [['a',1],['b',2],['c',3],['d',4]]
list2 =  [['e',5],['f',6],['g',7],['h',8]]

Desired Output:
out = [['a',1],['e',5],['b',2],['f',6],['c',3],['g',7],['d',4] ,['h',8]]

I have done:
def mergePreserveOrder(*argv):      
    for arg in argv:  
        for arg2 in argv: 
            if(len(arg) != len(arg2)) :
                print("arrays size do not match" + str(arg) +  str(arg2))                
                return 
    output = []    
    for index in range (len(argv[0])):    
        for arg in argv:
            output.append(arg[index])    
    return  output

mergePreserveOrder (list1 ,list2  )

A:

You can use zip along with chain.from_iterable:
list(chain.from_iterable(zip(list1, list2)))

Example:
from itertools import chain

list1 = [['a',1],['b',2],['c',3],['d',4]]
list2 = [['e',5],['f',6],['g',7],['h',8]]

print(list(chain.from_iterable(zip(list1, list2))))
# [['a', 1], ['e', 5], ['b', 2], ['f', 6], ['c', 3], ['g', 7], ['d', 4], ['h', 8]]

A:

The most general solution is the roundrobin recipe from the itertools module:
from itertools import cycle, islice

def roundrobin(*iterables):
    "roundrobin('ABC', 'D', 'EF') --> A D E B F C"
    # Recipe credited to George Sakkis
    num_active = len(iterables)
    nexts = cycle(iter(it).__next__ for it in iterables)
    while num_active:
        try:
            for next in nexts:
                yield next()
        except StopIteration:
            # Remove the iterator we just exhausted from the cycle.
            num_active -= 1
            nexts = cycle(islice(nexts, num_active))

list1 =  [['a',1],['b',2],['c',3],['d',4]]
list2 =  [['e',5],['f',6],['g',7],['h',8]]

print(list(roundrobin(list1, list2)))

which produces your desired output.
Unlike chain+zip, this solution will work with uneven length inputs, where zip will silently drop all elements beyond the length of the shortest input.

