2
$\begingroup$

How can this rule

a -> {1, 2, 3}

be transformed into this

{a -> 1, a -> 2, a -> 3}

by way of a function?

Answers 3175 and 10524 only address extracting values from the right-hand side, so (as far as I can tell) this question is distinct.

$\endgroup$
1
  • 1
    $\begingroup$ I don't really understand what you mean by "without referring to the symbol/string on the left-hand side explicitly in the code". Can you give an example? It would be best to have an example input and an example output. $\endgroup$
    – Szabolcs
    Commented Nov 14, 2014 at 20:30

1 Answer 1

4
$\begingroup$

Use Thread

Thread[ a -> {1, 2, 3} ]

and

Join @@ Thread /@ {a -> {1, 2, 3}, b -> {4, 5, 6}}

You might also be interested in this.

$\endgroup$
7
  • $\begingroup$ How would this be done without knowing that "a" is "a". In your answer, you refer to "a" in your code. I'll update the question to make it clearer. A related question would be: How do you extract the left-hand side of a rule efficiently? $\endgroup$
    – higgy
    Commented Nov 14, 2014 at 20:27
  • $\begingroup$ @higgy a -> b is just an alternate notation for Rule[a,b], so you can extract a using First, Part, Extract, etc. See here: reference.wolfram.com/language/tutorial/PartsOfExpressions.html $\endgroup$
    – Szabolcs
    Commented Nov 14, 2014 at 20:29
  • $\begingroup$ If extracting the left-hand side is a necessary step in all possible solutions to this question, then edit your answer I and will accept it. I'm also curious as to whether there are solutions that don't require extracting the left-hand side first, i.e., something more direct. $\endgroup$
    – higgy
    Commented Nov 14, 2014 at 20:34
  • $\begingroup$ @higgy You don't need to know the first part of the rule to apply Thread to it. If you have x = a -> {1,2,3} then you can just use Thread[x], no need to know what's inside x, other than that it's a rule with a list on the RHS. Can you give an example input and a corresponding example output? I thought a -> {1, 2, 3} and {a -> 1, a -> 2, a -> 3} could be considered such. Thread takes you from one to the other. $\endgroup$
    – Szabolcs
    Commented Nov 14, 2014 at 20:35
  • 1
    $\begingroup$ @higgy No, it's not necessary. I think we're having a communication breakdown. Why don't you join chat? $\endgroup$
    – Szabolcs
    Commented Nov 14, 2014 at 20:36

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.