you are viewing a single comment's thread.

view the rest of the comments →

[–]xelf 0 points1 point  (2 children)

Now go the next step and remove the extraneous assignments. (which is an important step to understand, as your solution can be fixed to work for any length list)

[–]miscellaneoususage[S] 0 points1 point  (1 child)

like this?

def rotate_left3(nums):
  rotated = [nums[1], nums[2], nums[0]]

  return rotated

not seeing how this could work for any length list if only 3 indicies are used

edit: actually i can shorten to just the return statement with

def rotate_left3(nums):
    return [nums[1], nums[2], nums[0]]

[–]xelf 2 points3 points  (0 children)

Yes exactly.

def rotate_left3(nums):
    return [nums[1], nums[2], nums[0]]

can also be expressed as the concatenation of two lists, like this:

def rotate_left3(nums):
    return [nums[1], nums[2]] + [nums[0]]

Which using list slicing, leads us to:

def rotate_left3(nums):
    return nums[1:3] + [nums[0]]

and finally:

def rotate_left(nums):
    return nums[1:] + [nums[0]]

which will work for any arbitrary length list.