Here my (tongue on the cheek) answers:
myDict = reduce(lambda d, t: (d.__setitem__(t[1], d.get(t[1], 0) + t[2]), d)[1], myTupleList, {})
This is ugly and bad, but here's how it works.
The first argument to reduce (because it is not clear there) is lambda d, t: (d.__setitem__(t[1], d.get(t[1], 0) + t[2]), d)[1] . I'll talk about this later, but for now, I'll just call it joe (don't be offended by any of the people named Joe). The reduction function basically works as follows:
joe(joe(joe({}, myTupleList[0]), myTupleList[1]), myTupleList[2])
And this is for a list of three elements. As you can see, it mainly uses its first argument to copy each result into the final answer. In this case, the final answer is the dictionary you wanted.
Now for joe . Here joe as def :
def joe(myDict, tupleItem): myDict[tupleItem[1]] = myDict.get(tupleItem[1], 0) + tupleItem[2] return myDict
Unfortunately, in Python lambda , the form = or return not allowed, so you need to find it. I am lacking = by calling the dict __setitem__ function directly. I do without returning by creating a tuple with the return value __setitem__ and a dictionary, and then returning the tuple element containing the dictionary. I will change joe slowly so you can see how I did it.
First remove = :
def joe(myDict, tupleItem):
Then, so that the whole expression is evaluated to the value that we want to return:
def joe(myDict, tupleItem): return (myDict.__setitem__(tupleItem[1], myDict.get(tupleItem[1], 0) + tupleItem[2]), myDict)[1]
I used this use case to reduce and dict many times in my Python programming. In my opinion, dict can use the reduceto(keyfunc, reduce_func, iterable, default_val=None) member reduceto(keyfunc, reduce_func, iterable, default_val=None) . keyfunc will take the current value from the iteration and return the key. reduce_func will take the existing value in the dictionary and the value from the iterable and return the new value for the dictionary. default_val will be passed to reduce_func if there is no key in the dictionary. The return value must be the dictionary itself, so you can do things like:
myDict = dict().reduceto(lambda t: t[1], lambda o, t: o + t, myTupleList, 0)