Ad

takes two lists that are already sorted, and merges them together.

another simple solution but with higher order (O) is to simply sorted(a + b) the two lists.

def merge_lists(a, b):
    """
    takes two sorted lists, and merges them
    """
    sorted_list = []
    # grab inital values
    a_i = 0
    b_i = 0
    # while lists are not empty
    while len(a) != a_i and len(b) != b_i:
        if a[a_i] < b[b_i]:
            sorted_list.append(a[a_i])
            a_i += 1
        else:
            sorted_list.append(b[b_i])
            b_i += 1
    # append whatever is remaining
    [sorted_list.append(i) for i in a[a_i:]]
    [sorted_list.append(i) for i in b[b_i:]]
    return sorted_list