4

I know how to RotateRight a single list; lets take the following example:

list=Range[5];
rot=RotateRight[list,2]

But how to make the same rotation on a bunch of lists like:

list={Range[5],Range[5]};

The result should look like:

rot={{4,5,1,2,3},{4,5,1,2,3}}
Kay
  • 1,035
  • 7
  • 19
  • 1
    I vote to close this question as this can be quite easily found in the documentation for RotateRightand also here. – gwr Aug 18 '16 at 10:47

3 Answers3

7

For RotateRight you can specify levelspec by using a list in the 2nd argument:

list = {Range[5], Range[5]};
RotateRight[list, {0, 2}]

{{4, 5, 1, 2, 3}, {4, 5, 1, 2, 3}}

gwr
  • 13,452
  • 2
  • 47
  • 78
Coolwater
  • 20,257
  • 3
  • 35
  • 64
  • 1
    +1. This is imho the most concise answer to the question as no other constructs are needed to fulfill the task - RotateRight is well able to do it by itself given the right parametrization. – gwr Aug 18 '16 at 09:26
  • I agree with @gwr. I didn't think about giving a list as additional argument. Nice that this works... However, the general solution from e.doroskevic might be of interest too. – Kay Aug 18 '16 at 10:20
  • @gwr I think the reason for the diversity of answers is manifold. 1. the question must be asked properly (especially difficult for non-native speakers) 2. Sometimes the example given is just a MWE. The problem might be even more complicated. Thats why a general solution could be more useful. 3. – Kay Aug 18 '16 at 11:00
  • 1
    @Kay Sure, but you may see that this specific answer is more general than mapping RotateRight with one parameter? Sometimes it simply pays off to wait with an accept to give other solutions a chance - rather than to strive for instant gratification... :) – gwr Aug 18 '16 at 11:04
  • @gwr I'm sure instant gratification isn't possible for theat reason :D But thanks for your comments! I'll think about this – Kay Aug 18 '16 at 11:06
3

Example

list = {Range[5], Range[5]};
RotateRight[#, 2] & /@ list

Output

{{4, 5, 1, 2, 3}, {4, 5, 1, 2, 3}}

Reference

Map
Pure Function

Additional Resources

Where can I find examples of good Mathematica programming practices
What are the most common pitfalls awaiting new users

e.doroskevic
  • 5,959
  • 1
  • 13
  • 32
  • Exactly, what I was looking for! Thank you! I will study the syntax, as I'm still a bit unfamiliar with '&' and '/@', though I know it means 'Function' and 'Map'... – Kay Aug 18 '16 at 09:07
  • @Key I will add a reference section covering those and some additional link to some useful information on the subject :) glad it was helpful – e.doroskevic Aug 18 '16 at 09:12
2

Or you could use ConstantArray[] directly

ConstantArray[RotateRight[Range[5], 2], 2]
xyz
  • 605
  • 4
  • 38
  • 117