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.

This question already has an answer here:

I have a list of the form:

lstA = { 
         { {"a","b"}, -1.5}, 
         { {"c","d"}, -2  }   
       }

I wanto to add another value to each sublist that comes from another list

lstB = {-5,-10}

I would like the final list to look like

 mergeList =  { 
            { {"a","b"}, -1.5, -5 }, 
            { {"c","d"}, -2,-10 }
            }

I have tried using Append to get each sublist, and then append the value corresponding value from lstB:

  Append[{#1, #2} & @@@ lstA, lstB] 
share|improve this question

marked as duplicate by Mr.Wizard Apr 30 at 2:10

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
Have you looked at Insert or do you need Append? –  Öskå Apr 29 at 16:18
    
I have been trying Append. Append should work because I want to get each sublist then append the value from lstB –  olliepower Apr 29 at 16:19
    
Very closely related: (13748) –  Mr.Wizard Apr 30 at 2:01

3 Answers 3

up vote 7 down vote accepted

The most efficient approach uses (new in ver.6) ArrayFlatten:

ArrayFlatten[{{lstA, Transpose[{lstB}]}}]
{{{"a", "b"}, -1.5, -5}, {{"c", "d"}, -2, -10}}

Instead of threading Append which is not very efficient, another much faster way would use also Transpose twice and Append only once:

Transpose @ Append[ Transpose @ lstA, lstB]

or alternatively one could exploit the second argument of Join using Transpose only once:

Join[ lstA, Transpose[{lstB}], 2]

In the Front-end Transpose can be is rewritten (using Esc+ tr + Esc) in especially terse (neat) way, e.g.:

enter image description here

Moreover we have:

enter image description here

share|improve this answer

You can do that using MapThread:

mergeList = MapThread[Append, {lstA, lstB}]
share|improve this answer
4  
Happy 10K!..... –  Michael E2 Apr 29 at 16:30
    
@MichaelE2: Thank you. –  celtschk Apr 29 at 16:30

Since I like to stick with my ideas, here is an example with Insert:

Insert[lstA[[#1]], #2, #3] & @@@ Thread[{Range@Length@lstA, lstB, {3, 3}}]
{{{"a", "b"}, -1.5, -5}, {{"c", "d"}, -2, -10}}
share|improve this answer

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