Splicing of lists

This recipe splices any number of nonempty lists as one list. Each input list may be of different length. Elements of shorter lists may be recycled when the keyword argument recycle is set to True. Any error result in an empty output list.


Splicing of two lists of same length without using explicit looping were discussed in comp.lang.python message threads 2006-05-07 00:28:44. This recipe generalizes splicing to more than two lists which may be of different lengths.

def  splice(alists, recycle = True):
    """
    Accepts a list of nonempty lists or indexable objects in
    argument alists (each element list may not be of the same
    length) and a keyword argument recycle which
    if true will reuse elements in lists of shorter length.

    Any error will result in an empty list to be returned.
    """

    try:
        nlists = len(alists)
        lens   = [len(alist) for alist in alists]
        if not recycle:
            totlen = sum(lens)
        else:
            totlen = max(lens) * nlists
            
        pos  = [0] * nlists
        R    = [None] * totlen
        i, j = 0, 0
        while i < totlen:
            if pos[j] < lens[j]:
                R[i]     = alists[j][pos[j]]
                i        += 1
                pos[j] = pos[j] + 1
                if recycle and pos[j] >= lens[j]:
                    pos[j] = 0
            j = (j + 1) % nlists
        return R
    except:
        return []

if __name__ == "__main__":
    print splice([[1,2,3], ['a','b'], [4], [-1,-2,-3,-4]], recycle = False)
    print splice([[1,2,3], ['a','b'], [4], [-1,-2,-3,-4]])

    
        
        
    

code.activestate.com ↗