Python List pop() method
Description
The pop() function removes an element from a list (the last element by default) and returns its value.
Syntax
pop() method syntax:
list.pop([index=-1])
Parameters
- obj -- optional parameter, the index of the list element to remove; it cannot exceed the total length of the list. It defaults to index=-1, which removes the last list value.
Return Value
This method returns the element object removed from the list.
Example
The following example shows how to use the pop() function:
Example
#!/usr/bin/python3
#coding=utf-8
list1
=
[
'
'
,
'
Runoob
'
,
'
Taobao
'
]
list_pop
=
list1
.
pop
(
1
)
"
The removed item is:
"
,
list_pop
"
The list is now:
"
,
list1
The output of the above example is:
删除的项为 : Runoob
列表现在为 : ['Google', 'Taobao']
Python Lists