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

Python3 List append() Method

Python3 List append() Method

Python3 Lists The Python3 list


Description

The append() method adds a new object to the end of a list.

Syntax

append() method syntax:


list.append(obj)

Parameters

Return Value

This method returns nothing, but modifies the original list.

Example

The following example shows how to use the append() function:

Example

#!/usr/bin/python3

list1

=

[

'Google'

,

'Runoob'

,

'Taobao'

]

list1.

append

(

'Baidu'

)

print

(

"Updated list: "

,

list1

)

The output of the above example is:


更新后的列表 :  ['Google', 'Runoob', 'Taobao', 'Baidu']

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