如何从列表中删除第一个项目? [英] How to remove the first Item from a list?
本文介绍了如何从列表中删除第一个项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个列表[0, 1, 2, 3, 4]
,我想将其放入[1, 2, 3, 4]
.我该怎么办?
I have the list [0, 1, 2, 3, 4]
I'd like to make it into [1, 2, 3, 4]
. How do I go about this?
推荐答案
list.pop(index)
>>> l = ['a', 'b', 'c', 'd']
>>> l.pop(0)
'a'
>>> l
['b', 'c', 'd']
>>>
删除列表[索引]
>>> l = ['a', 'b', 'c', 'd']
>>> del l[0]
>>> l
['b', 'c', 'd']
>>>
这两个都修改了您的原始列表.
These both modify your original list.
其他人建议使用切片:
- 复制列表
- 可以返回一个子集
此外,如果要执行许多pop(0),则应查看 collections.deque
Also, if you are performing many pop(0), you should look at collections.deque
from collections import deque
>>> l = deque(['a', 'b', 'c', 'd'])
>>> l.popleft()
'a'
>>> l
deque(['b', 'c', 'd'])
- 提供更高的性能,从列表的左端弹出
这篇关于如何从列表中删除第一个项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文