How do I fix this syntax error with list slicing

Viewed 21

I am trying to use list splicing to rotate a value in a list but I can't figure out why my brackets are not closing. the issue in question is on line 3. It is throwing an invalid syntax error saying that "[" is not closed code is below

def rotate_list(data, amount):
    result = []
    result.append(data = [amount:])

    return result
print(rotate_list([1,2,3,4,5,6,7,8,9],5)) # [5, 6, 7, 8, 9, 1, 2, 3, 4]
print(rotate_list([1,2,3,4,5,6,7,8,9],9)) # [1, 2, 3, 4, 5, 6, 7, 8, 9]
1 Answers

[amount:] is not syntactically correct, as you are missing the list in which to iterate. Try rewriting your function as

def rotate_list(data, amount):
      return data[amount:] + data[:amount] 
Related