Re: Sum elements in a list with conditions

• To: mathgroup at smc.vnet.net
• Subject: [mg69849] Re: Sum elements in a list with conditions
• From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
• Date: Mon, 25 Sep 2006 03:53:23 -0400 (EDT)
• Organization: The Open University, Milton Keynes, UK
• References: <ef5066\$mm5\$1@smc.vnet.net>

```Guillermo Sanchez wrote:
> Dear group
> I have a pair of elements list. The second elements of the list should
> be summed when the first element of the pairs are equals. Example
>
> Given
>
> {{a1, b1},{a2,b2},{a2,b3},{a2,b4},{a3, b5}, {a3, b6}}
>
> the output should be
>
> {{a1, b1},{a2, b2+b3+b4},{a3, b5+b6}}
>
> Thanks
>
> Guillermo
>

lst = {{a1, b1}, {a2, b2}, {a2, b3}, {a2, b4}, {a3, b5}, {a3, b6}};

({#1[[1,1]], Plus @@ #1[[All,2]]} & ) /@ Split[lst, #1[[1]] == #2[[1]]&]

--> {{a1, b1}, {a2, b2 + b3 + b4}, {a3, b5 + b6}}

Regards,
Jean-Marc

```

• Prev by Date: RE: Sum elements in a list with conditions
• Next by Date: Re: Sum elements in a list with conditions
• Previous by thread: RE: Sum elements in a list with conditions
• Next by thread: Re: Sum elements in a list with conditions