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

前端 未结 12 1673
天涯浪人
天涯浪人 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:09

    If you are allergic to slice notation: a.insert(0,a.pop())

    Usage:

    In [15]: z=[1,2,3]
    
    In [16]: z.insert(0,z.pop())
    
    In [17]: z
    Out[17]: [3, 1, 2]
    
    In [18]: z.insert(0,z.pop())
    
    In [19]: z
    Out[19]: [2, 3, 1]
    

提交回复
热议问题