A list rotation consists of taking the first element and moving it to the end. For instance, if we rotate the list [1,2,3,4,5], we get [2,3,4,5,1]. If we rotate it again, we get [3,4,5,1,2].
Here are some examples to show how your function should work.
rotatelist([1,2,3,4,5],1)
[2, 3, 4, 5, 1]
rotatelist([1,2,3,4,5],3)
[4, 5, 1, 2, 3]
rotatelist([1,2,3,4,5],12)
[3, 4, 5, 1, 2]
I tried to code it successfully but had a problem, when I concatenate the list, I get the error: int iterable error but when I use append
the program executes successfully, kindly explain the concept here is my python code:
def rotatelist(l,k):
if k<0:
return l
new_list=l[::]
while k>0:
temp=new_list[0]
new_list=new_list[1:]
new_list=new_list+list(temp)
k-=1
return new_list