Shift list elements to the right and shift list element at the end to the beginning

前端 未结 12 1677
天涯浪人
天涯浪人 2021-02-08 08:43

I want to rotate elements in a list, e.g. - shift the list elements to the right so [\'a\',\'b\',\'c\',\'d\'] would become [\'d\',\'a\',\'b\',\'c\'], o

12条回答
  •  长情又很酷
    2021-02-08 09:00

    Solution for your query :

    def shift(aList):
        l = list()
        n = len(aList)
        l.append(aList[-1])
        temp = aList[:n-1]
        for i in temp:
            l.append(i)
        return l
    print(shift(aList=[1,2,3]))
    

提交回复
热议问题