Python3 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
- index -- optional parameter, the index of the element to remove; it must not exceed the list length. Defaults to index=-1, which removes the last value in the list.
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
list1
=
[
'Google'
,
'Runoob'
,
'Taobao'
]
list1.
pop
(
)
(
"The list is now: "
,
list1
)
list1.
pop
(
1
)
(
"The list is now: "
,
list1
)
The output of the above example is:
列表现在为 : ['Google', 'Runoob']
列表现在为 : ['Google']
A queue is a first-in-first-out (FIFO) data structure; we can use a list to implement its basic functionality.
append()The method adds an element to the end of the queue.pop()The method removes an element from the front of the queue and returns it.
Example
queue
=
[
]
Add elements to the end of the queue
queue.
append
(
'A'
)
queue.
append
(
'B'
)
queue.
append
(
'C'
)
Remove elements from the front of the queue and return them
(
queue.
pop
(
0
)
)
A
(
queue.
pop
(
0
)
)
B
(
queue.
pop
(
0
)
)
C
In the example above, we create an empty list as a queue, then use append() to add three elements to the end of the queue. Next we use pop() to remove elements from the front and return them. Because a queue is a first-in-first-out data structure, the output is 'A', 'B', and 'C'.
The Python3 list