Python3 List append() Method
Description
The append() method adds a new object to the end of a list.
Syntax
append() method syntax:
list.append(obj)
Parameters
- obj -- the object to add to the end of the list.
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'
)
(
"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.
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