TransWikia.com

Join two lists by using the first element

Mathematica Asked on July 27, 2020

I have two lists, which as a simple example can take the form:

l1 = {{a1,a2,a3},{b1,b2,b3},{c1,c2,c3}}
l2 = {{a1,n1},{c1,n2}}

How can I merge them by the first element in the list to obtain a list of the form

newList = {{a1,a2,a3,n1},{c1,c2,c3,n2}}.

I tried

GroupBy[Join[l1,l2],First->Last]

but this will only return the last elements of each (e.g. {{a1,a3,n1},{c1,c3,n2}, how do I keep all the elements? Thanks.

One Answer

l1 = {{a1, a2, a3}, {b1, b2, b3}, {c1, c2, c3}};
l2 = {{a1, n1}, {c1, n2}};

l3 = {{a1, n1}, {c1, n2}, {a1, n3}};

merge[l1_List, l2_List] := 
 Join[#[[1]], Rest[##][[All, -1]]] & /@
  Select[GatherBy[Join[l1, l2], First], Length[#] > 1 &]

merge[l1, l2]

(* {{a1, a2, a3, n1}, {c1, c2, c3, n2}} *)

merge[l1, l3]

(* {{a1, a2, a3, n1, n3}, {c1, c2, c3, n2}} *)

Correct answer by Bob Hanlon on July 27, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP