Summing a 2D list

BJörn Lindqvist bjourne at gmail.com
Fri Jun 13 14:54:11 CEST 2008


On Fri, Jun 13, 2008 at 2:12 PM, Karsten Heymann
<karsten.heymann at blue-cable.net> wrote:
> Although your problem has already been solved, I'd like to present a
> different approach which can be quite a bit faster. The most common
> approach seems to be using a dictionary:
>
> summed_up={}
> for user,vote in pairs:
>  if summed_up.has_key(user):
>    summed_up[user]+=vote
>  else:
>    summed_up[user]=vote

You'll save even more by using:

if user in summed_up:

instead of has_key.

-- 
mvh Björn



More information about the Python-list mailing list