If you really want to rotate an array, there are generally two ways to do this. The first is more pythonic and involves appending the end of the list to the front. The second way is to utilize the collections library and rotate a list wrapped in a deque object.
''' Rotate an array of elements
'''
from collections import deque
from datetime import datetime
# Approach #1
def rotate_1(arr, amount):
return arr[amount:] + arr[:amount] # append the front of the list to the back
# Approach #2
def rotate_2(arr, amount):
shifted = deque(arr) # wrap list in an deque
shifted.rotate(amount * -1) # rotate normally works left-to-right
return list(shifted) # unwrap the list
months = list(range(1, 13)) # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
if __name__ == '__main__':
offset = datetime.now().month - 1 # 1 (curent month indexed at 0)
months = rotate_2(months, offset) # [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1]
months = rotate_2(months, -3) # [11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(months)