5
$\begingroup$

I have two lists as follow:

l={a,b,c,d}

and

ll={{1,x},{2,x},{3,x},{4,x}}

I want to replace the x's with a,b,c and d so that the outcome would be:

{{1,a},{2,b},{3,c},{4,d}}

How would one does this?

$\endgroup$
7
$\begingroup$

This should make the job:

ll[[;; , 2]] = l;
ll

{{1, a}, {2, b}, {3, c}, {4, d}}

Or equivalently:

MapThread[#1 /. {x -> #2} &, {ll, l}]

{{1, a}, {2, b}, {3, c}, {4, d}}

Note that the output is the same, but what the code is doing is different, and you might prefer one solution or the other depending on your specific case.

$\endgroup$
5
$\begingroup$
Transpose[{ll[[All, 1]], l}]

{{1, a}, {2, b}, {3, c}, {4, d}}

My preferred method would be the first method proposed in Fraccalo's answer.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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