On 23 June 2010 00:06, Neil Webster <nswebs...@gmail.com> wrote: > Hi all, > > I've got a simple problem but it's defeated me and I was wondering if > somebody could point out where I'm going wrong or offer an alternative > solution to the problem? > > I have a list of lists such as [[a,2,3,4],[b,10,11,12], [a,2,3,4]]. I > need to combine the two lists that have the same first character in > this example 'a'. In reality there are 656 lists within the list. >
Here's my take: >>> input = [['a',2,3,4], ['b',10,11,12], ['a',2,3,4]] >>> output = {} >>> for i in input: ... output[i[0]] = output.get(i[0], 0) + sum(i[1:]) ... >>> output {'a': 18, 'b': 33} Note in your example, a and b are not defined, so I used a string instead. Cheers, Xav
-- http://mail.python.org/mailman/listinfo/python-list