Python

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.

How to change Left & Right mouse buttons on Windows 10 PC
It's quite a norm that all computer mouse devices are ergonomically designed for right-handed users. But there are mouse devices available which are s...
Emulate Mouse clicks by hovering using Clickless Mouse in Windows 10
Using a mouse or keyboard in the wrong posture of excessive usage can result in a lot of health issues, including strain, carpal tunnel syndrome, and ...
Add Mouse gestures to Windows 10 using these free tools
In recent years computers and operating systems have greatly evolved. There was a time when users had to use commands to navigate through file manager...