2
$\begingroup$

Consider the following nested list

{{a,1/x},{b,2x},{c,Sqrt[x]},{d,1-1/x}}

I'd like to come up with the same list where the variable $x$ is systematically replaced by the other element present in the same sublist. Here is the list I am trying to create

{{a,1/a},{b,2b},{c,Sqrt[c]},{d,1-1/d}}

How can I achieve this?

$\endgroup$
2
$\begingroup$
lst = {{a, 1/x}, {b, 2 x}, {c, Sqrt[x]}, {d, 1 - 1/x}}

{#1, #2 /. x -> #1} & @@@ lst
(* {{a, 1/a}, {b, 2 b}, {c, Sqrt[c]}, {d, 1 - 1/d}} *)

or

lst /. {a_, b_} :> {a, b /. x -> a}
 (* {{a, 1/a}, {b, 2 b}, {c, Sqrt[c]}, {d, 1 - 1/d}} *)
$\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.