Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python3 List pop() Method

Python3 List pop() Method

Python3 Lists The Python3 list


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

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

(

)

print

(

"The list is now: "

,

list1

)

list1.

pop

(

1

)

print

(

"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.

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

print

(

queue.

pop

(

0

)

)

 

A

print

(

queue.

pop

(

0

)

)

 

B

print

(

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'.

Python3 Lists The Python3 list