append()
function is used to Add an item to the end of the list.
Syntax of append()
Function
The syntax of append()
function in Python is:
list.append(x)
Parameters of append()
Function in Python
x
– Where x
is any item (number, strings, another list, dictionary) which is used to appended in the list. This parameter is required.
Compatibility
append()
function is available and compatible with both Python 2.x
and 3.x
. This function is one of python list methods.
Return Value of append()
Function in Python
append()
function does not return anything.
Python append()
Function Example 1
Adding an item to a list.
fruits = ['apple', 'mango', 'cherry', 'orange'] fruits.append('banana') #Updated fruits list print(fruits)
Output:
['apple', 'mango', 'cherry', 'orange', 'banana']
Python append()
Function Example 2
Adding a list to another list.
# fruits list fruits1 = ['apple', 'mango', 'cherry', 'orange'] # another fruits list fruits2 = ['banana', 'lime'] # adding on fruits list to another fruits list fruits1.append(fruits2) #Updated fruits list print(fruits1)
Output:
['apple', 'mango', 'cherry', 'orange', ['banana', 'lime']]