def partition(iterable, pivot, key):
    """Returns 3 lists: the lists of smaller, equal and larger elements, compared to pivot.
      Elements are compared by applying the key function and comparing the results"""
    pkey = key(pivot)
    less = []
    equal = []
    greater = []
    for x in iterable:
        k = key(x)
        if k < pkey:
            less.append(x)
        elif k == pkey:
            equal.append(x)
        else:
            greater.append(x)
    return less, equal, greater