Index all *except* one item in python

前端 未结 9 1046
清酒与你
清酒与你 2020-11-28 23:45

Is there a simple way to index all elements of a list (or array, or whatever) except for a particular index? E.g.,

  • mylist[3]

9条回答
  •  温柔的废话
    2020-11-28 23:51

    The simplest way I found was:

    mylist[:x] + mylist[x+1:]
    

    that will produce your mylist without the element at index x.

    Example

    mylist = [0, 1, 2, 3, 4, 5]
    x = 3
    mylist[:x] + mylist[x+1:]
    

    Result produced

    mylist = [0, 1, 2, 4, 5]
    

提交回复
热议问题