-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
1
  • 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. Commented Jul 20, 2019 at 8:14

4 Answers 4

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]
Sign up to request clarification or add additional context in comments.

Comments

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]

3 Comments

why not to use list(temp) the list function also converts the items present in it as list
because you are trying to make a list with an integer. I have added 2 more examples in the answer.
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
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)

Comments

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]

3 Comments

why not to use list(temp) the list function also converts the items present in it as list
@AnkushRasgon your deque example will not produce expected result - a negative number has to be used in .rotate().
@PranavSharma [] just wraps temp in a list. list(temp) converts it to a list.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.