Code that makes cyclic reference for x spaces in list
<p>I have a tasko to make a program in which i get m, n and k. I should create a list a with <code>n*m</code> element.
List <code>b</code> is supposed to have <code>n*m</code> element. It is created from list a with cyclic shift k to the right for m elements of lists. 
I know it is poorly explained. Example is:</p>

<pre><code>n=3
m=4
A=1 2 3 4 5 6 7 8 9 10 11 12
k=1
B=4 1 2 3 8 5 6 7 12 9 10 11
</code></pre>

<p>What i have at the moment is:</p>

<pre><code>from random import randint
n = int(input())
m=int(input())

A = []
B=[0]
B=B*n*m
for i in range(n*m):
    A = A + [randint(1, 30)]

print('\nLista A:\n')
for i in range(n*m):
    print(A[i], end = ' ')

print()

k=int(input())

for i in range(-1, m*n, m):
    B[m-1-i]=A[i]
    print(B[m-1-i])

print('\nLista B:\n')
for i in range(n*m):
    print(B[i], end = ' ')
</code></pre>

<p>Thanks</p>

<p>Alternative:</p>

<pre><code>m=4
n=3
k=1

A=list(range(1,1+m*n))
print (A)

t_1=[A[_:_+4] for _ in range(0,len(A), 4)]
print (t_1)

t_2=[]
for sublist in t_1:
    t_2.append(sublist[-k:]+sublist[:-k])
print (t_2)

B=[]
for sublist in t_2:
    B.extend(sublist)

print (B)
</code></pre>

<p>If you want greater speed then you could use a <strong>deque</strong> from the <strong>collections</strong> module to build <strong>t_2</strong>.</p>

<p>Here's the result.</p>

<pre><code>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
[[4, 1, 2, 3], [8, 5, 6, 7], [12, 9, 10, 11]]
[4, 1, 2, 3, 8, 5, 6, 7, 12, 9, 10, 11]
</code></pre>

python list cyclic-reference