if not pred(seq[start]):
    break
start += 1
while True:
    if pred(seq[end - 1]):
        break
end -= 1
if start == end:
    return start
seq[start], seq[end - 1] = seq[end - 1], seq[start]
start += 1
end -= 1
Fast way to get N Min or Max elements from a list in Python