Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Say I have a set of lists that look like this:

list = {{{151.335`, 245.102`}, {1, 1}}, {{41.435`, 245.021`}, {2, 2}, {3, 3}},
        {{131.048`, 243.364`}, {3, 3}, {56, 56}, {76, 23}}, {{164.911`, 244.039`}, 
         {4, 4}}, {{98.1685`, 239.618`}, {5, 5}}, {{196.333`, 239.212`}, {6, 6}}, 
        {{184.767`, 234.228`}, {7, 7}}, {{213.044`, 234.24`}, {8, 8}}, 
        {{26.6316`, 221.423`}, {9, 9}}};

appendList = {{{3, 3}, {"Yellow", "Red"}, {"Blue", "Majesty"}},
              {{76, 23}, {"White", "Avalanche"}}}

How can I use the first element in each sublist of appendList as an anchor to append the next set of elements in place in list generating something like the following?:

finalList = {{{151.335`, 245.102`}, {1, 1}}, {{41.435`, 245.021`}, {2, 2}, {3, 3},
              {"Yellow","Red"},{"Blue","Majesty"}}, {{131.048`, 243.364`}, {3, 3},
              {56, 56}, {76, 23},{"White","Avalanche"}}, {{164.911`, 244.039`}, 
              {4, 4}}, {{98.1685`, 239.618`}, {5, 5}}, {{196.333`, 239.212`},{6,6}},
             {{184.767`, 234.228`}, {7, 7}}, {{213.044`, 234.24`}, {8, 8}}, 
             {{26.6316`, 221.423`}, {9, 9}}};

After-the-fact question:

This is a brief followup which doesn't quite deserve it's own question - how can we only look at the last element in each sublist of list to check to see if it matches the "anchor" value at the first position in appendList?

In other words, where {{{2, 2}, {"Yellow", "Red"}, {"Blue", "Majesty"}}, {{56, 56}, {"White", "Avalanche"}}} yields a finalList = list since {2,2} and {56,56} are not at the ends of the sublists in list?

share|improve this question

3 Answers 3

up vote 6 down vote accepted

One way to do this is the following:

list /. (RuleDelayed[{x__, #[[1]]}, {x}~Join~#] & /@ appendList)
share|improve this answer

Another approach is using Sequence

list /. (#[[1]] -> Sequence @@ # & /@ appendList)
share|improve this answer
    
Ah, the parentheses seem to make it work! –  HStoley Sep 13 '13 at 20:01
    
Do you know a quick fix to address my "after-the-fact question" added to the question body? It seems simple, but I haven't been able to figure out how to do it with a simple one-liner. –  HStoley Sep 13 '13 at 20:02
    
@HStoley I'm already thinking about it. In this case my solution transforms to list /. ({x___, #[[1]]} :> {x, Sequence @@ #} & /@ appendList) which is equivalent to tom's solution :) –  ybeltukov Sep 13 '13 at 20:07
    
Ah, so Tom's solution automatically does this? –  HStoley Sep 13 '13 at 20:08
    
And thanks for thinking about the update. –  HStoley Sep 13 '13 at 20:09

Here is my variation of tom's solution. (It too handles the "after-the-fact" addition.)
I use ## (SlotSequence) and @@@ (Apply at level one) for more concise code.

list /. ({x___, #} :> {x, ##} & @@@ appendList)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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