-2

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
NelsonGon
  • 13,015
  • 7
  • 27
  • 57
  • `list` takes an iterable as parameter, and you are passing it an `int`. Instead of `list(temp)`, you should write `[temp]` in order to get a list containing this unique value. – Thierry Lathuille Jul 20 '19 at 08:14

4 Answers4

1
def rotatelist(l, n):
    n = n % len(l)
    return l[n:]  + l[:n]

print(rotatelist([1,2,3,4,5], 12))

Prints:

[3, 4, 5, 1, 2]
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
0
from collections import deque
d = deque([1,2,3,4,5])
d.rotate(-3)
Ouput:
[4, 5, 1, 2, 3]

OR in your case

def rotatelist(l,k):
    if k<0:
        print(l)
    new_list=l[::]    
    while k>0:
        temp=new_list[0]
        new_list=new_list[1:]
        new_list=new_list+[temp]
        k-=1
    print(new_list)

rotatelist([1,2,3,4,5],3)

Output:

[4, 5, 1, 2, 3]
AnkushRasgon
  • 782
  • 1
  • 6
  • 14
0

You can do it with deque and its rotate method.

from collections import deque


def rotatelist(l, k):
    dq = deque(l)
    dq.rotate(-k)
    return list(dq)
Dušan Maďar
  • 9,269
  • 5
  • 49
  • 64
0

this 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.append(temp)
        k-=1
    return new_list

or this one:

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+[temp]
        k-=1
    return new_list

will produce the following output:

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]   

You have the error because you are trying to make a list with an integer:

temp = 1
print(list(temp))

output:

TypeError: 'int' object is not iterable 

The example below works fine because you are putting an integer inside a list:

temp = 1
print([temp])

output:

[1]
bart
  • 1,003
  • 11
  • 24
  • why not to use list(temp) the list function also converts the items present in it as list – Pranav Sharma Jul 20 '19 at 08:29
  • because you are trying to make a list with an integer. I have added 2 more examples in the answer. – bart Jul 20 '19 at 08:40
  • ok i got it in list () we can only work on the iterable objects like strings,tuples,dict etc as int is non iterable we can not use in list() but we either make it list using [] or use append , thanks for your cooperation – Pranav Sharma Jul 20 '19 at 09:23