Python3 List append() Method

Python3 列表Python3 Lists


Description

The append() method is used to add a new object at the end of the list.

Syntax

Syntax of the append() method:

list.append(obj)

Parameters

  • obj -- The object to be added to the end of the list.

Return Value

This method has no return value, but it modifies the original list.

Example

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

Example

#!/usr/bin/python3

list1 = ['Google', 'Example', 'Taobao']
list1.append('Baidu')
print ("Updated list : ", list1)

The output of the above example is as follows:

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

A queue is a first-in, first-out (FIFO) data structure. We can use a list to implement the basic functionality of a queue.

  • 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
print(queue.pop(0))  # A
print(queue.pop(0))  # B
print(queue.pop(0))  # C

In the above example, we created an empty list as a queue, then used the append() method to add three elements to the end of the queue. Next, we used the pop() method to remove elements from the front of the queue and return them. Since a queue is a first-in, first-out data structure, the output we get is 'A', 'B', and 'C'.

Python3 列表Python3 Lists

Other Extensions