Function to merge sorted lists

John Goerzen jgoerzen at complete.org
Thu Sep 6 13:51:30 EDT 2001


"Edward C. Jones" <edcjones at erols.com> writes:

> Here is a Python program that does the same thing:

There's a more straightforward way to do it:

def merge(l1, l2):
    ret = []
    l2c = 0
    for item in l1:
        while l2c < len(l2) and l2[l2c] < item:
            ret.append(l2[l2c])
            l2c += 1
        ret.append(item)
    return ret + l2[l2c:]

-- 
John Goerzen <jgoerzen at complete.org>    GPG: 0x8A1D9A1F    www.complete.org



More information about the Python-list mailing list