Re: Howto sum up a list?

*To*: mathgroup at smc.vnet.net*Subject*: [mg106944] Re: Howto sum up a list?*From*: Raffy <raffy at mac.com>*Date*: Fri, 29 Jan 2010 07:45:58 -0500 (EST)*References*: <hjrf6g$mur$1@smc.vnet.net>

On Jan 27, 11:43 pm, Jonas Stein <n... at jonasstein.de> wrote: > How can i sum up the list: > > MyList = {{eggs, 4}, {milk, 1}, {eggs, 1}, {milk, 1}} > > I want to get something like: > > {{eggs, 5},{milk, 2}} > > I tyied Tally[], but that ignored the factor 4 in {eggs, 4} > > Thank you. > > -- > Jonas Stein <n... at jonasstein.de> I'd suggest: {#[[1, 1]], Total[#[[All, 2]]]} & /@ GatherBy[MyList, First] Or pre-v7: Last@Reap[Sow[#2, #1] & @@@ MyList, _, {#1, Total[#2]} &] Silly ideas: Apply[List, Times @@ Power @@@ MyList, {0, 1}] Tally@Flatten[ConstantArray @@@ MyList] MyList //. {a___, {x_, y_}, b___, {x_, z_}, c___} :> {a, {x, y + z}, b, c}