The list is one of the important data structures in Python that arranges the elements in a sequence. The append() is the built-in function in Python that inserts the element or items to the end of the list and essentially increases the length of the list by one. This article explains the Python list append() function with examples.
Syntax of the append() function
The syntax of the append() function is as follows:
listObj.append(item)
The append() function is called with the name of the list object. It only takes one parameter. We pass the item as an argument.
Examples
#creating a list of numbersmyList = [1,2,3,4,5,6]
#apending the list
myList.append(7)
#printing the updated list
print("The updated list is:", myList)
#creating a list of strings
myList = ['a','b','c','d']
#apending the list
myList.append('e')
myList.append('f')
myList.append('g')
#printing the updated list
print("The updated list is:", myList)
#creating a list of mix data types
myList = ['a',1,2,3,'b','c','d',5]
#apending the list
myList.append(6)
myList.append('Kamran')
myList.append('Sattar')
myList.append('Awaisi')
#printing the updated list
print("The updated list is:", myList)
Output
In the output it can be observed that all the items appended in the lists are added to the end of the lists.
We can also append a list in another list. The appended list be added at the end. Let's append a list in another list.
#creating a list of numbersmyList1 = [1,2,3,4,5,6]
#creating a list of strings
myList2 = ['a','b','c','d']
# appending the list
myList1.append(myList2)
print(myList1)
Output
Conclusion
Python provides many built-in data structures like lists, tuples, dictionaries, etc. The list arranges the items in sequence. We can add the elements in the list after declaring the list. The append() is a built-in function that inserts the items at the end of the list. This article explains the list append() function with examples.