在Python中使用索引删除项目的就地函数

刚刚注意到
Python中没有函数可以通过索引删除列表中的项目,以便在链接时使用.

例如,我正在寻找这样的东西:

another_list = list_of_items.remove [item-index]

代替

del list_of_items [item_index]

因为,remove(item_in_list)在删除item_in_list后返回列表;我想知道为什么索引的类似函数被遗漏了.似乎非常明显和微不足道的被包括在内,觉得有理由跳过它.

有关为什么这样的功能不可用的任何想法?

—–编辑——-

list_of_items.pop(item_at_index)不合适,因为它没有返回列表而没有要删除的特定项,因此不能用于链接. (根据文件:L.pop([index]) – > item – 删除并返回索引处的项目)

最佳答案 使用
list.pop

>>> a = [1,2,3,4]
>>> a.pop(2)
3
>>> a
[1, 2, 4]

根据文件:

s.pop([i])

same as x = s[i]; del s[i]; return x

UPDATE

对于链接,您可以使用以下技巧. (使用包含原始列表的临时序列):

>>> a = [1,2,3,4]
>>> [a.pop(2), a][1] # Remove the 3rd element of a and 'return' a
[1, 2, 4]
>>> a # Notice that a is changed
[1, 2, 4]
点赞