Lists are an important part of the Python programming language. pop()
function is used with lists and arrays in order to return the latest item from the list or array. In this tutorial we will learn how to use pop()
function removes the latest item or specified item from a list.
列表是Python编程语言的重要组成部分。 pop()
函数用于列表和数组,以便从列表或数组返回最新的项。 在本教程中,我们将学习如何使用pop()
函数从列表中删除最新的项目或指定的项目。
pop()函数语法 (pop() Function Syntax)
pop()
function has very simple syntax where we provide none or single argument.
pop()
函数的语法非常简单,我们不提供任何参数或仅提供单个参数。
LIST.pop(INDEX)
- `LIST` is the list, array or similar data type which stores multiple elements. LIST是存储多个元素的列表,数组或类似数据类型。
- `INDEX` is the index number of the item we want to remove. If not provided the latest item will be returned and removed from the LIST. In python, the index numbers start from 0, not 1. “ INDEX”是我们要删除的商品的索引号。 如果未提供,则将返回最新项目并将其从LIST中删除。 在python中,索引号从0开始,而不是1。
The pop()
function will return the item we want to pop. If an index number is provided which does not exist an error like IndexError: popo index out of range
error will be returned.
pop()
函数将返回我们要弹出的项目。 如果提供的索引号不存在,则会返回类似IndexError: popo index out of range
的错误IndexError: popo index out of range
error。
流行不带索引号 (Pop without and Index Number)
The most popular usage case is calling pop()
function without index number. This will return the latest item of the given list or array. In this following example, we will run pop() function two times.
最流行的用法是调用没有索引号的pop()
函数。 这将返回给定列表或数组的最新项。 在下面的示例中,我们将运行pop()函数两次。
mylist = ['Ankara','Istanbul','Canakale','London','Munih']
item = mylist.pop()
print(item)
# The output will be Munih
print(mylist)
# The output will be ['Ankara', 'Istanbul', 'Canakale', 'London']
item2 = mylist.pop()
print(item)
# The output will be Munih
print(mylist)
# The output will be ['Ankara', 'Istanbul', 'Canakale']