I have two lists, L1 and L2, each with some data and two scenarios:

  1. L1 = {{"a", {"aa", "ab"}}}; L2 = {{"a", {"ac", "aab", "aac"}}}. How do I align two lists to make {{"a", {"aa", "ab", "ac", "aab", "aac"}}}?

  2. L1 = {(*{"a",{"aa"}},*)}; L2 = {"a", {"ab", "ac", "aab", "aac"}} How do I align two lists to make {"a", {(*"aa",*)"ab", "ac", "aab", "aac"}}?

  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – Michael E2 Feb 15 '16 at 2:30
  • $\begingroup$ You can format inline code and code blocks by selecting the code and clicking the {} button above the edit window. The edit window help button ? is also useful for learning how to format your questions and answers. You may also find this this meta Q&A helpful $\endgroup$ – Michael E2 Feb 15 '16 at 2:30
  • 6
    $\begingroup$ Despite the fact that your question has an answer, I still find it entirely baffling. I think you may want to explain what you are trying to do much better, and possibly give some context. Also, your second version of L1 is essentially an empty list, since the contents are commented away. Is that really what you meant? $\endgroup$ – MarcoB Feb 15 '16 at 3:22
  • $\begingroup$ Well, the question asks for the process that the lists are going through to yield the final list. It was just that we are giving two separate lists, and we need to 'align' them to produce the final list. As for the second version, that is really what the question is like...so it meant what it looks like..thanks a lot in advance! $\endgroup$ – KaeMat Feb 15 '16 at 9:57
  • 1
    $\begingroup$ @KaeMat Please tell us the context. (**) - comments are not going to survive any evaluation unless the are inside string so what you want to get, doesn't make sense. Comments are comments. $\endgroup$ – Kuba Feb 15 '16 at 10:15

I am going to ignore scenario 2 because it makes no sense. There is no way to compute with comments.

For scenario 1, I recommend transforming your lists into associations, merging the associations, and transforming back to lists. It can be done like so.

data1 = {{"a", {"aa", "ab"}}, {"b", {"aa", "bb"}}};
data2 = {{"a", {"ac", "aab", "aac"}}, {"b", {"bc", "bb", "abb"}}};

merged = Merge[AssociationThread @@@ Transpose /@ {data1, data2}, Apply[Union]];
Apply[List, merged // Normal, {1}]
{{"a", {"aa", "aab", "aac", "ab", "ac"}}, {"b", {"aa", "abb", "bb", "bc"}}}

Ugly, but it works:

   Union[{L1[[1, 1]]}, {L2[[1, 1]]}], 
  {Union[L1[[1, 2]], L2[[1, 2]]]}]
  • $\begingroup$ Thank you very much for your answer! I was thinking if I could use any type of "alignment" in the process because the question specifically asks to 'align' the lists, and also there are also "b","c","d","e" with their own respective elements in both lists. I know if I union one at a time and then join them together, I could yield the same answer. But I was wondering if there is a more efficient way to accomplish it? Thanks a lot again! $\endgroup$ – KaeMat Feb 15 '16 at 9:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.