Question

I am just wondering if there's a syntax error on my end. I have an int list and I want to rotate them left. I used deque and rotate(-1) but it's returning None.

An example of outcome I am looking for:

list1 = [1, 2 , 3]
rotateLeft = [2, 3, 1]

This is the code snippet.

from collections import deque

def rotate_left(nums):        
    return deque(nums).rotate(-1)

print rotate_left([1, 2, 3])
>>> None

Can anyone tell me if I did something wrong?

Was it helpful?

Solution

deque.rotate returns nothing.

So rotate_left should be:

def rotate_left(nums):
    q = deque(nums)
    q.rotate(-1)
    return list(q)

Almost functions/methods in standard library that modify data return nothing. (Exception: dict.setdefault, ..)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top