# Replace all elements of a list

I have a set that looks like:

s = {{1, 2, 3} -> 9, {4, 1, 9} -> 9, {3, 7, 3} -> 1};


Now, I want to replace each element of the set by the lhs of "->". The result should look like this:

s = {{1, 2, 3}, {4, 1, 9}, {3, 7, 3}};


## The problem:

I only want to modify the list s without creating a new list. This is why Replace[] and Map[] cannot be used in this scenario.

-
list[[All, 1]] –  Öskå Dec 22 '13 at 13:20
btw. Map[First, {{1, 2, 3} -> 9, {4, 1, 9} -> 9, {3, 7, 3} -> 1}] does the job, but i want to REPLACE the elements. Thank you! –  user11284 Dec 22 '13 at 13:21
Replace[{{1, 2, 3} -> 9, {4, 1, 9} -> 9, {3, 7, 3} -> 1}, x_ :> First[x], {1}] ? –  andre Dec 22 '13 at 13:31
Your question is not clear: Oska's answer and your Map and andres answer all accomplish the task as you've asked it. If this isn't what you want, you need to be more explicit. –  bill s Dec 22 '13 at 13:32
Welcome to Mathematica.SE. I agree with bill's concern; I have closed this post to answers to head off people answering the wrong question. Please clarify what you are actually trying to do. I will then reopen it. –  Mr.Wizard Dec 22 '13 at 13:37

You state that you want to modify the original rule list rather than creating a new list. I assume that you want to avoid copying the list for memory reasons. I think you are unlikely to do better than the simple list = list[[All, 1]] recommended by Öskå. We can see that the additional memory used is fairly small, at least with the type of sample rules you gave:

\$HistoryLength = 0;

big = MapThread[Rule, {RandomInteger[9, {5*^6, 3}], RandomInteger[99, 5*^6]}];

MaxMemoryUsed[]

1614925768


After the operation:

big = big[[All, 1]];
MaxMemoryUsed[]

1700726928


Using First by comparison (in a separate kernel):

big = First /@ big;
MaxMemoryUsed[]

1940364440


Part only uses an additional ~5.3% RAM.

-

Another possibility, discussed several times, e.g. in passing large list by reference, is to use the "trick" of holding the argument of a function to pass it by reference.

ClearAll[f];
Attributes[f] = {HoldFirst};
f[li_] := Do[li[[i]] = First[li[[i]]], {i, Length[li]}];

f[s]


produces the desired outcome of modifying the list in place as you desired, without copying it.

-