shift_right python using for loops

前端 未结 3 1897
自闭症患者
自闭症患者 2021-01-26 04:18

The question is to write a shift_right function so that it shifts every element in the list to the right. For instance, if the list is

L = [\'a\',\         


        
3条回答
  •  说谎
    说谎 (楼主)
    2021-01-26 04:36

    I'd implement it like this:

    def shift_right(L):
        if len(L) > 0:
            L.insert(0, L.pop())
    

    As Lee correctly comments, this is a rotate operation rather then a shift.

提交回复
热议问题