

def sum_of_three(nums):
    nums.sort()
    results = []
    for i, el in enumerate(nums):
        if i == 0 or el != nums[i - 1]:
            for l, r in sum_of_two(nums, -el, i + 1):
                results.append((nums[i], l, r))

    return list(results)


def sum_of_two(nums, el, left):
    right = len(nums) - 1
    while left < right:
        tot = nums[left] + nums[right]
        if tot == el:
            yield nums[left], nums[right]
        if tot < el:
            left += 1
            while left < len(nums) and nums[left] == nums[left - 1]:
                left += 1
        if tot >= el:
            right -= 1
            while right >= 0 and nums[right] == nums[right + 1]:
                right -= 1


if __name__ == '__main__':
    print sum_of_three([-1, 0, 1, 2, -1, -4])