7

With a nested list such as

listA = {{{a,b,c},{1,0,0}}, {{a,b,c},{0,1,0}}, {{d,e,f},{1,1,0}}};

How can I create

listB = {{{a,b,c},{{1,0,0},{0,1,0}}}, {{d,e,f},{1,1,0}}};

and so-forth for any length of listA?

kglr
  • 394,356
  • 18
  • 477
  • 896
Phillip Dukes
  • 938
  • 5
  • 18

1 Answers1

7
KeyValueMap[List] @ GroupBy[listA, First -> Last] 
Values @ GroupBy[listA, First, {#[[1, 1]], #[[All, 2]]}&] 
KeyValueMap[List] @ Merge[Association /@ Rule @@@ listA, Identity]
{#[[1, 1]], #[[All, 2]]} & /@ GatherBy[listA, First]

all give

{{{a, b, c}, {{1, 0, 0}, {0, 1, 0}}}, {{d, e, f}, {{1, 1, 0}}}}

kglr
  • 394,356
  • 18
  • 477
  • 896