4
$\begingroup$

I have an association of the following form :

assoc = Association[a -> {1, 2, 3}, b -> {5, 6, 7}]

<|a -> {1, 2, 3}, b -> {5, 6, 7}|>

(the size of actual lists in assoc is 30).

If I want to change the value of the list associated to a in the following manner: assoc[a][[1]] = 2, it does not work. I understand why mostly, I have looked it up (some mutability stuff e_e) !

by searching the forum, I didn't find any better than:

temp=assoc[a]
temp[[1]]=2
AssociateTo[assoc,a->temp]

I have two questions :

Between changing in place the value of a very big list (very) or changing the value of a very big association like demonstrated above, which is faster ?

Second, is there a better way to change the value of elements of the list associated to key a in assoc ?i.e. without the copying process.

Thank you.

$\endgroup$
1
  • 4
    $\begingroup$ You need to use single Part call and wrap the key(s) in Key: assoc[[Key[a], 1]] = 2 $\endgroup$ Apr 30, 2020 at 18:23

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.