[英]How do I remove the first item from a list?
How do I remove the first item from a list?如何从列表中删除第一项?
[0, 1, 2, 3] → [1, 2, 3]
You can find a short collection of useful list functions here .您可以在此处找到有用的列表函数的简短集合。
>>> 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.这些都修改了您的原始列表。
Others have suggested using slicing:其他人建议使用切片:
Also, if you are performing many pop(0)
, you should look at collections.deque
此外,如果您正在执行许多
pop(0)
,您应该查看collections.deque
from collections import deque
>>> l = deque(['a', 'b', 'c', 'd'])
>>> l.popleft()
'a'
>>> l
deque(['b', 'c', 'd'])
Slicing:切片:
x = [0,1,2,3,4]
x = x[1:]
Which would actually return a subset of the original but not modify it.这实际上会返回原始的子集但不会修改它。
you would just do this你会这样做
l = [0, 1, 2, 3, 4]
l.pop(0)
or l = l[1:]
或
l = l[1:]
Pros and Cons利弊
Using pop you can retrieve the value使用 pop 你可以检索值
say x = l.pop(0)
x
would be 0
说
x = l.pop(0)
x
将是0
Then just delete it:然后删除它:
x = [0, 1, 2, 3, 4]
del x[0]
print x
# [1, 2, 3, 4]
You can use list.reverse()
to reverse the list, then list.pop()
to remove the last element, for example:您可以使用
list.reverse()
反转列表,然后使用list.pop()
删除最后一个元素,例如:
l = [0, 1, 2, 3, 4]
l.reverse()
print l
[4, 3, 2, 1, 0]
l.pop()
0
l.pop()
1
l.pop()
2
l.pop()
3
l.pop()
4
You can also use list.remove(a[0])
to pop
out the first element in the list.您还可以使用
list.remove(a[0])
pop
列表中的第一个元素。
>>>> a=[1,2,3,4,5]
>>>> a.remove(a[0])
>>>> print a
>>>> [2,3,4,5]
You could use unpacking assignment as mentioned in PEP 3132 .您可以使用PEP 3132中提到的解包分配。
You should try unpacking like the following:您应该尝试像下面这样解包:
>>> l = [0, 1, 2, 3, 4]
>>> _, *l = l
>>> l
[1, 2, 3, 4]
As mentioned in PEP 3132 :如PEP 3132中所述:
This PEP proposes a change to iterable unpacking syntax, allowing to specify a "catch-all" name which will be assigned a list of all items not assigned to a "regular" name.
此 PEP 提议更改可迭代的解包语法,允许指定一个“包罗万象”的名称,该名称将被分配一个未分配给“常规”名称的所有项目的列表。
An example says more than a thousand words:
一个例子说一千多个字:
>>> a, *b, c = range(5) >>> a 0 >>> c 4 >>> b [1, 2, 3]
There is a data structure called deque
or double ended queue which is faster and efficient than a list.有一种称为
deque
或双端队列的数据结构,它比列表更快更高效。 You can use your list and convert it to deque and do the required transformations in it.您可以使用您的列表并将其转换为双端队列并在其中执行所需的转换。 You can also convert the deque back to list.
您还可以将双端队列转换回列表。
import collections
mylist = [0, 1, 2, 3, 4]
#make a deque from your list
de = collections.deque(mylist)
#you can remove from a deque from either left side or right side
de.popleft()
print(de)
#you can covert the deque back to list
mylist = list(de)
print(mylist)
Deque also provides very useful functions like inserting elements to either side of the list or to any specific index. Deque 还提供了非常有用的功能,例如将元素插入到列表的任一侧或任何特定索引。 You can also rotate or reverse a deque.
您还可以旋转或反转双端队列。 Give it a try!
试试看!
This works for me, instead of using pop
like below, which of course will be 0, because the result/return value of pop
这对我有用,而不是像下面那样使用
pop
,它当然是 0,因为pop
的结果/返回值
>>> x = [0, 1, 2, 3].pop(0)
>>> x
0
Using this below method will skip the first value:使用下面的方法将跳过第一个值:
>>> x = [0, 1, 2, 3][1:]
>>> x
[1, 2, 3]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.