Efficient way to rotate a list in python

后端 未结 26 1611
一生所求
一生所求 2020-11-22 03:14

What is the most efficient way to rotate a list in python? Right now I have something like this:

>>> def rotate(l, n):
...     return l[n:] + l[:n]         


        
26条回答
  •  独厮守ぢ
    2020-11-22 03:47

    I think you've got the most efficient way

    def shift(l,n):
        n = n % len(l)  
        return l[-U:] + l[:-U]
    

提交回复
热议问题