A conise python solution based on ksum
class Solution:\n        # @return a list of lists of length 4, [[val1,val2,val3,val4]]\n        def fourSum(self, num, target):\n            num.sort()\n      
    class Solution:\n        # @return a list of lists of length 4, [[val1,val2,val3,val4]]\n        def fourSum(self, num, target):\n            num.sort()\n            def ksum(num, k, target):\n                i = 0\n                result = set()\n                if k == 2:\n                    j = len(num) - 1\n                    while i < j:\n                        if num[i] + num[j] == target:\n                            result.add((num[i], num[j]))\n                            i += 1\n                        elif num[i] + num[j] > target:\n                            j -= 1\n                        else:\n                            i += 1\n                else:\n                    while i < len(num) - k + 1:\n                        newtarget = target - num[i]\n                        subresult = ksum(num[i+1:], k - 1, newtarget)\n                        if subresult:\n                            result = result | set( (num[i],) + nr for nr in subresult)\n                        i += 1\n                    \n                return result\n            \n            return [list(t) for t in ksum(num, 4, target)]
[]