I have a function

MinVarGeneralThermal1[T, 5, d]

which yields me the following list:

{{0.00917615, 0.0136892, 0.0204219, 0.0304659, 0.0454498, 0.10115,
 0.225114, 0.335831, 0.150899, 0.0678031}}

As you can see these are not single pairs of entires of the form


which makes it not possible for me to target single entries using a command like

MinVarGeneralThermal1[T, 5, d][[3]]

Is it possible to write a swap command that works something like this

swap[MinVarGeneralThermal1[T, 5, d],{1,3}]

which would essentially swap the first and third entries of the list (for example) ? In the end I want to be able to swap any two entries I want.

Thanks in advance !


3 Answers 3


I am not sure that I fully get your problem. You can always access individual elements of the nested list you have, e.g. by using yourList[[1, 3]].

However, this may work for you:

swap[list_List, pair_?(VectorQ[#, IntegerQ] &)] := {Permute[list[[1]], Cycles[{pair}]]}

Here is an example usage on a made-up list:

list = {CharacterRange["a", "g"]}
swap[list, {2, 3}]

(* Out:
{{"a", "b", "c", "d", "e", "f", "g"}}
{{"a", "c", "b", "d", "e", "f", "g"}}
sWap[a : {{__}}, pair_] :=  Module[{l = a}, l[[1, pair]] = l[[1, Reverse @ pair]]; l]

MinVarGeneralThermal1[T, 5, d]


sWap[MinVarGeneralThermal1[T, 5, d], {1, 3}]


list = CharacterRange["a", "g"];

SubsetMap[RotateLeft, list, {2, 3}]

{"a", "c", "b", "d", "e", "f", "g"}

To swap more than one element:

SubsetMap[RotateLeft, list, {2, 3, 4}]

{"a", "c", "d", "b", "e", "f", "g"}

If you want to start with the double brackets:

list = {CharacterRange["a", "g"]};

{SubsetMap[RotateLeft, list[[1]], {2, 3}]}

{{"a", "c", "b", "d", "e", "f", "g"}}


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.