Home>Article>Backend Development> 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.
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!