Home > Backend Development > Python Tutorial > What does pop mean in python?

What does pop mean in python?

爱喝马黛茶的安东尼
Release: 2019-06-25 11:05:23
Original
46653 people have browsed it

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])
Copy after login

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);
Copy after login

The output results of the above examples are as follows:

A List :  abc
B List :  zara
Copy after login

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)
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template