Home > Article > Backend Development > What does pop mean in python?
The pop() function is used to remove an element from the list (the last element by default) and return the value of the element.
pop() method syntax:
list.pop(obj=list[-1])
Parameters
obj -- optional parameter, to remove list elements Object.
Return value
This method returns the element object removed from the list.
Related recommendations: "Python Video Tutorial"
Example
The following example shows how to use the pop() function :
#!/usr/bin/python aList = [123, 'xyz', 'zara', 'abc']; print "A List : ", aList.pop(); print "B List : ", aList.pop(2);
The output results of the above examples are as follows:
A List : abc B List : zara
Tips for using the pop function
1. The pop() function is mainly used in lists ( list), remove elements from the list, and implement the function through the subscript value. By default, the last element of the list is removed, and only one element can be removed at a time. If you want to remove the first element of the list, you only need pop(0) [using the attribute whose subscript value starts from 0] to achieve its function.
2. Based on the above functional description, you can combine the while loop to implement stack loop and queue loop, see the code
for i in range(5) urlList.append(i) # 模拟先进的过程 while urlList: #判断list是否为空 url = urlList.pop(0) #实现了先出的效果, 结合上面列表的创建过程,实现了先进先出的效果,就是队列 print(url) while urlList: #判断list是否为空 url = urlList.pop() #实现了后出的效果, 结合上面列表的创建过程,实现了先进后出的效果,就是栈 print(url)
The above is the detailed content of What does pop mean in python?. For more information, please follow other related articles on the PHP Chinese website!