Possible Duplicate:
How to Set parts of indexed lists?

For better organization of my code, sometime I use data objects in the form:

data["A", "1"] = {{1, 2}, {3, 4}}

instead of

dataA1 = {{1, 2}, {3, 4}};

One of the advantages is when working with Module, because I can declare data just one time, and I can use data["A","1"], data["A","2"], data["B","1"] and so on, with limited scope.

Everything works fine except for one detail. With data["A", "1"] I can't perform operations like:

data["A", "1"] = {{1, 2}, {3, 4}}
data["A", "1"][[All, 2]] = "X"

to change data["A", "1"] to {{1, "X"}, {3, "X"}}

I got the error:

Set::setps: data["A", "1"] in the part assignment is not a symbol

When I try with:

dataA1 = {{1, 2}, {3, 4}};
dataA1[[All, 2]] = "X";

I have no problem and get {{1, "X"}, {3, "X"}} as expected.

There is nome way to perform this kind of operation in data["A", "1"] object?


The question indicated as duplicated do not handle the case: data["A", "1",...] but just data["A"]

  • $\begingroup$ See also this Mathgroup thread $\endgroup$ Jan 19, 2013 at 15:32
  • $\begingroup$ @LeonidShifrin tks!.. Should I delete this? $\endgroup$
    – Murta
    Jan 19, 2013 at 15:41
  • 2
    $\begingroup$ Not sure about deletion. It may be better to close this but keep it alive. If you make a slightly more specific / descriptive title, it may drive future visitors to that question. I take it that you tried to find something on the topic before asking, and did not find that one. And I only found it because I remember answering it. So, right now I am for closing, let's see what others think. $\endgroup$ Jan 19, 2013 at 15:46
  • $\begingroup$ @LeonidShifrin hi!.. I'm studying Upvalues and I just saw that your answer and Mr.Wisard too do not apply to my case data["A", "1"] they work for just one arg function. It's not the case to reopen it? Or it's simple to extend it for data[__] case? $\endgroup$
    – Murta
    Jan 25, 2013 at 14:03
  • 1
    $\begingroup$ Hello! In my answer to that question, simply replace everywhere sym[index_] with sym[indices__] (of course, consistently across the bodies of the functions), and it should work, I think. Let me know if it does not. As for the answer of @Mr.Wizard, I suppose that should work for it too. $\endgroup$ Jan 25, 2013 at 14:26

1 Answer 1


You may use:

 data["A", "1"] = ReplacePart[data["A", "1"], {_, 2} -> "X"]

It transforms data["A", "1"] from {{1, 2}, {3, 4}} to {{1, "X"}, {3, "X"}}.

  • 3
    $\begingroup$ The problem with this is that ReplacePart copies entire expression, so a single element assignment using ReplacePart will not have a constant time complexity. For most applications, this will be a disaster. $\endgroup$ Jan 19, 2013 at 15:38
  • $\begingroup$ I agree that this should only be used when computing time is insignifiant $\endgroup$
    – andre314
    Jan 19, 2013 at 15:59

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