Move an item inside a list?

前端 未结 6 503
-上瘾入骨i
-上瘾入骨i 2020-11-29 01:58

In Python, how do I move an item to a definite index in a list?

6条回答
  •  独厮守ぢ
    2020-11-29 02:43

    A slightly shorter solution, that only moves the item to the end, not anywhere is this:

    l += [l.pop(0)]
    

    For example:

    >>> l = [1,2,3,4,5]
    >>> l += [l.pop(0)]
    >>> l
    [2, 3, 4, 5, 1]
    

提交回复
热议问题