Suppose we have the following list:

{5, 3}

I want to add the -> to the end of the first element in the list in Mathematica programmatically and then add the list of {2,3} to the end of it.

What I want to achieve is to have something that I would have if I typed it directly as the following:


Actually, I have the components, and I want to have them assembled in a way that Mathematica understands as an input command.

  • $\begingroup$ The answers below yield your specific case. Are they general enough, or are there things that you need that won't be covered by those examples? $\endgroup$ – march Jul 24 '15 at 16:41
  • $\begingroup$ @march What is provided is just an example. I want to make Mathematica to behave like it has been typed as a command. $\endgroup$ – Qaher Jul 24 '15 at 16:48
  • $\begingroup$ The answers below give you an output that can be used as an input, so I'm confused by your clarification. Can you give more details as to what you're doing? $\endgroup$ – march Jul 24 '15 at 17:00
  • $\begingroup$ There are several ways: Replace/ReplaceAll, ReplacePart, MapAt, Delete+Insert, etc. $\endgroup$ – Szabolcs Jul 24 '15 at 17:02
  • $\begingroup$ Did one of the solutions below answer your question? If so, please accept one so that we can take it off the unanswered list. If not, please let us know why it doesn't work, and we will attempt to fix the answer. $\endgroup$ – march Aug 20 '15 at 5:07

Is this the sort of thing you meant?

   mix[{a_, b_}, lis_] := {a -> lis, b}
   mix[{5, 3}, {2, 3}]
(* {5 -> {2, 3}, 3} *)

Or did you have in mind some relationship between the argument I denoted lis and the pair constituting the first argument?

createRule[x_, y_] := MapAt[# -> y &, x, 1]


createRule[{5, 3}, {2, 3}]

results in

{5 -> {2, 3}, 3}

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.