Home >Backend Development >Python Tutorial >How to use pop() function in python

How to use pop() function in python

王林
王林Original
2020-05-10 13:24:436620browse

How to use pop() function 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.

Syntax:

list.pop(obj=list[-1])       //默认为 index=-1,删除最后一个列表值。
//obj -- 可选参数,要移除列表元素的对象。

This method returns the element object removed from the list.

Example:

sentence=['All', 'good', 'things', 'come', 'to' ,'those', 'who', 'wait.']

print("默认为 index=-1,删除最后一个列表值:",sentence.pop(-1),"\n")
print("默认删除最后一个列表值: ",sentence.pop(),"\n")
print("删除第一个元素:",sentence.pop(0),"\n")
print("删除第三个元素:",sentence.pop(2),"\n")
print("输出剩余元素:",sentence)

The operation result is: (This method returns the element object removed from the list.) Each time print() is executed, one is removed.

How to use pop() function in python

Recommended tutorial: python tutorial

The above is the detailed content of How to use pop() function in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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