Piton

Python List Append

Python List Append

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 numbers
myList = [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 numbers
myList1 = [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.

OpenTTD vs Simutrans
Creating your own transport simulation can be fun, relaxing and extremely enticing. That's why you need to make sure that you try out as many games as...
OpenTTD Tutorial
OpenTTD is one of the most popular business simulation games out there. In this game, you need to create a wonderful transportation business. However,...
SuperTuxKart for Linux
SuperTuxKart is a great title designed to bring you the Mario Kart experience free of charge on your Linux system. It is pretty challenging and fun to...