1
$\begingroup$

I have the two lists $A=\{1,2,3\}, B=\{a,b,c\}$. I want to get a new list $C=\{1 -> a,2->b,3->c\}$, which is just adding $->$ sign in between.

I tried

A={1,2,3};
B={a,b,c};
C={};
W="->";
Do [
 f[i]=A[[i]]*W*B[[i]];
 AppendTo[C, f[i]];
 ,{i,1,3}
];

But it is flipping the sequence in $C$ list. How to get rid of this?

$\endgroup$
1
  • $\begingroup$ What are you expecting the * to do? $\endgroup$
    – Ghoster
    Commented Jul 15, 2023 at 0:07

4 Answers 4

5
$\begingroup$
Thread[{1, 2, 3} -> {a, b, c}]
(* {1 -> a, 2 -> b, 3 -> c} *)

Note that the string "->" is different than -> and multiplying isn't the same as inserting. Also, it is generally recommended to avoid using upper-case variables, as they may conflict with built-in symbols. In your case, A and B are safe, but you cannot assign to C because it is protected as a built-in symbol.

$\endgroup$
2
$\begingroup$

Actually you may also see <| |>

AssociationThread[{1,2,3},{a,b,c}]

gives an Association

if you do want list

AssociationThread[{1,2,3},{a,b,c}] // Normal
$\endgroup$
2
$\begingroup$
Inner[Rule, {1, 2, 3}, {a, b, c}, List]

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

$\endgroup$
1
$\begingroup$

Another minor variant can be:

Clear["Global`*"];
alist = {1, 2, 3};
blist = {a, b, c};
Rule @@@ Transpose[{alist, blist}]

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

$\endgroup$

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