2
$\begingroup$

I am a beginner. I have a function with one parameter and I would like to replace a list of values that have names. I would like a result so that the names are not lost.

values = {a-> 1, b-> 2};

If I ignore the names, I get what I want:

Evaluate[x/6 /.x -> {1, 2}]
(*  {1/6, 1/3}  *)

But I would like to get a result like:

{a -> 1/6, b -> 1/3}

Is it possible?

$\endgroup$
2
  • $\begingroup$ sorry, formatting is lost, so readability of my question is not very good. $\endgroup$
    – schd
    Commented Dec 10, 2020 at 13:51
  • $\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 useful for learning how to format your questions and answers. You may also find this meta Q&A helpful $\endgroup$
    – Michael E2
    Commented Dec 10, 2020 at 14:06

2 Answers 2

2
$\begingroup$
values = {a -> 1, b -> 2};
asso = Association[values]
(#/6) & /@ asso // Normal
$\endgroup$
1
  • $\begingroup$ thanks, it works! $\endgroup$
    – schd
    Commented Dec 11, 2020 at 9:44
1
$\begingroup$
values = {a -> 1, b -> 2};
Association[values]/6
<|a -> 1/6, b -> 1/3|>
Normal @ %
{a -> 1/6, b -> 1/3}

Also

values /. x_?NumericQ :> x/6

MapAt[#/6 &, values, {All, 2}]

SubsetMap[#/6&, values, {All ,2}]

ReplacePart[values, {i_,2}:> values[[i,2]]/6]

Module[{v = #}, v[[All, 2]] = v[[All, 2]]/6; v] &@values

all give

{a -> 1/6, b -> 1/3}
$\endgroup$
1
  • $\begingroup$ Thanks, it was helpful! $\endgroup$
    – schd
    Commented Dec 11, 2020 at 9:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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