# Rotate elements in a list using a for loop

I have the following list.

data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};


I want to replace (rotate) each element using a for loop. In the first step, the first element is rotated. In the second step, the first two elements are rotated. Then after 4 steps of the loop, the output should look like: I tried several options, for example:

ReplacePart[data,
Thread[data[[1 ;; 3]] -> Rotate[data[[1 ;; 3]], 90 Degree]]]


But this replacement doesn't give me the output I want. How can I perform this sequence of rotations?

• The question arises: what for? Jun 27 at 10:12
• …Well, why do you mention for loop? You don't even use it yourself. Jun 29 at 6:04

With a for loop:

data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} ;
size = Length[data] ;
table = {} ;
For[
i = 1,
i <= size,
i++,
data[[i]] = Rotate[data[[i]], 90 Degree] ;
AppendTo[table, data] ;
] ;
Grid[table, Frame->All]


Will this work? Or do you have to use Thread? How about using a loop? ClearAll["Global*"]
data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
newData=First@Last@Reap@Do[
z=Rotate[data[[#]],90 Degree]&/@Range[n];
Sow[Join[z,data[[n+1;;]]]],
{n,1,Length@data}
];
Grid[newData,Frame->All]

f = ReplaceList[ {a__, b___} :> Join[Map[Rotate[#, π/2] &]@{a}, {b}]];

f @ data // MatrixForm Take[f @ data, 5] // MatrixForm Alternatively, define a function with two arguments specifying the input list and desired number of rows to take:

ClearAll[g]
g = ReplaceList[{a__, b___} /; Length[{a}] <= #2 :>
Join[Map[Rotate[#, π/2] &] @ {a}, {b}]] @ # &;

g[data, 7] // MatrixForm Another way using LowerTriangularize:

rotateMat[lst_?VectorQ] := Module[{mat, pos},mat = ConstantArray[lst, Length[lst]];
pos = Join @@ (Position[LowerTriangularize[mat], #] & /@ lst);
ReplacePart[mat, Thread[pos -> Map[Rotate[#, π/2] &, Extract[mat, pos]]]]]


For example:

data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

rotateMat[data] // MatrixForm data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

Rest@FoldList[MapAt[Rotate[#,90 Degree]&,#1,#2]&,data,Range]//MatrixForm As a Grid

Grid[Rest@FoldList[MapAt[Rotate[#,90 Degree]&,#1,#2]&,data,Range[Length@data]],
Frame->All]


Another way

Rest@FoldList[ReplaceAt[#1,x_->Rotate[x,90 Degree],#2]&,data,Range@Length@data]
//Grid[#,Frame->All]&


Just for Fun

MapAt[Rotate[#,90 Degree]&, data,Transpose[{#}]]&/@Range@Range@Length@data
//Grid[#, Frame->All]&


Or

ReplaceAt[data,x_->Rotate[x,90 Degree],Transpose[{#}]]&/@Range@Range@Length@data
//Grid[#,Frame->All]& Let's package up a function to rotate a given number of elements in a list:

RotateElems[count_, list_] := MapAt[Rotate[#, Pi/2] &, list, List /@ Range@count]


Now we can generate a table of partially rotated lists:

Table[RotateElems[i, data], {i, Length@data}]


You can use Grid for display if you want:

Grid[Table[RotateElems[i, data], {i, Length@data}], Frame -> All] You could add an argument to RotateElems to specify the amount of rotation.

Here's a solution with SubsetMap:

f[expr_Rotate] := expr
f[expr_] := Rotate[expr, 90 Degree]
SetAttributes[f, Listable]

With[{n = 10, step = 5}, SubsetMap[f, Range@n, ;; #] & /@ Range@step] //
Grid[#, Frame -> All] & Shoutout to @E. Chan-López for the idea to use *Triangularize.

data = ConstantArray[Range, 10];
rotatedData = Map[Rotate[#, \[Pi]/2] &, data, {-1}];
LowerTriangularize@rotatedData + UpperTriangularize[data, 1] // Grid[#, Frame -> All] & Probably the simplest way:

Table[If[i >= j, Rotate[j, 90 Degree], j], {i,1,5}, {j,1,8}] // Grid
` 