I am trying to do the following:

S = GroupElements[SymmetricGroup[4]];
Nest[PermutationProduct[S[[2]], S[[#]]] &, 2, 3]

But this yields the following errors:

  • Part::pkspec1: The expression Cycles[{}] cannot be used as a part specification.

  • Part::pkspec1: The expression Cycles[{{3,4}}][PermutationProduct]{Cycles[{}],Cycles[{{3,4}}],Cycles[{{2,3}}],Cycles[{{2,3,4}}],Cycles[{{2,4,3}}],Cycles[{{2,4}}],Cycles[{{1,2}}],Cycles[{{1,2},{3,4}}],Cycles[{{1,2,3}}],Cycles[{{1,2,3,4}}],<<14>>}[[Cycles[{}]]] cannot be used as a part specification.

I don't understand what is happening, I don't know why it is using Cycles[{}] as part specification, shouldn't it be using an integer as part specification?

  • 1
    $\begingroup$ If you look at S it consists of a long list starting with: {Cycles[{}], Cycles[{{3, 4}}], Cycles[{{2, 3}}]...} $\endgroup$
    – bill s
    May 5, 2021 at 12:06
  • $\begingroup$ @bills Yes, it should take another element of that list and apply the permutation product. If I write PermutationProduct[S[[2]],S[[2]]], it computes without problems. $\endgroup$
    – Red Banana
    May 5, 2021 at 12:14

1 Answer 1


From the Nest documentation,


gives an expression with f applied n times to expr.

Your code starts off by defining

S = GroupElements[SymmetricGroup[4]];

which equals

{Cycles[{}], Cycles[{{3, 4}}], Cycles[{{2, 3}}], Cycles[{{2, 3, 4}}], ... }.

With this in place, when you attempt to do

Nest[f, 2, 3]


f = PermutationProduct[S[[2]], S[[#]]] &

the code does three things in sequence:

  • First, it applies f to 2, which returns f[2] = PermutationProduct[S[[2]], S[[#]]] &[2] = PermutationProduct[S[[2]], S[[2]]] = PermutationProduct[Cycles[{{3, 4}}], Cycles[{{3, 4}}]] = Cycles[{}].

  • It then attempts to apply f to that previous result, so it tries to calculate f[Cycles[{}]], and this equals PermutationProduct[S[[2]], S[[#]]] &[Cycles[{}]], which simplifies to PermutationProduct[S[[2]], S[[ Cycles[{}] ]]].

    This is where the code jams up -- you are trying to calculate S[[ Cycles[{}] ]], i.e., the Cycles[{}]-th part of S, and that's not going to work. The whole thing returns itself, unevaluated, and this is where the error message

    Part::pkspec1: The expression Cycles[{}] cannot be used as a part specification.

    gets generated.

  • As a third step, the code then attempts to nest f a third time, i.e., it tries to calculate PermutationProduct[S[[2]], S[[ x ]]], where the part specification x is now the result from the previous step, i.e., an unevaluated mess with head PermutationProduct. This generates the second, messier Part::pkspec1 error message.

In other words, your code is mostly asking Mathematica to do calculations which don't really make sense. Unfortunately, the code itself doesn't really give enough information about what you really want to calculate, so it's hard to give a definitive answer about how to fix it.

As my best guess, what you want is to take S[[2]] and then multiply it repeatedly with S[[2]]? If so, the correct expression would be

Nest[PermutationProduct[S[[2]], #] &, S[[2]], 3]

which starts off with a group element as the expr input of Nest, and then applies a function, g=PermutationProduct[S[[2]], #] & which has the property that it always returns a group element when fed a group element, so it can be correctly nested.

(Alternatively, if you want to work at the level of integers, you can try to use Position to recover which element of S got returned by the PermutationProduct, so using something like

Nest[Position[S, PermutationProduct[S[[2]], S[[#]]]][[1, 1]] &, 2, 3]

(where the [[1,1]] is to break the list returned by Position, in the hopes that it only ever carries a single element). But this is messier and more fragile, and I wouldn't recommend it much.)

  • $\begingroup$ Yes, you guessed correctly. Thanks for the reply. This is so odd, whenever # appears, something is going to behave in some very unexpected way, perhaps due to my low level of expertise in Mathematica, I am not very fluent in this. $\endgroup$
    – Red Banana
    May 5, 2021 at 18:38
  • 2
    $\begingroup$ @BillyRubina If that's the case, then I would recommend switching from the shorthand notation f[#]& to the equivalent notation Function[x,f[x]], using the full form of Function. This will force you to give the variable (here, x) a suitably descriptive name, which is useful discipline to guard against this type of slip-up. $\endgroup$ May 5, 2021 at 19:21

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.