With Append Python Method you can Appends objects at the end of the existing list. Python List append() Method is an inbuild function. It now returns a new list, rather it updates the original list. Using an append method in the list you can add numbers, strings, dictionary, another list, etc objects.
The Syntax for the append() method is :
obj −, This is the object to be appended on the list.
Add the new number in a list and print() both statement, before and after append()
aList = [1, 2, 3, 4] print(aList) aList.append(5) print("Updated List : ", aList)
Output : [1, 2, 3, 4]
Updated List : [1, 2, 3, 4, 5]
This example Adding a new element to the
colors list: Where the list is containing a string data type.
colors = ['red', 'black', 'white'] colors.append("pink") print(colors)
Output : [‘red’, ‘black’, ‘white’, ‘pink’]
Example Python List append() – Add a list to a list :
Here is an example of how to use append() method in python List.
list1 = ["apple", "banana", "cherry"] list2 = ["red", "yellow", "red"] list1.append(list2) print(list1)
Output : [‘apple’, ‘banana’, ‘cherry’, [‘red’, ‘yellow’, ‘red’]]
Do comment if any doubt and suggestion regarding this tutorial.
Note : This example (Project) is developed in PyCharm 2018.2 (Community Edition)
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
All Examples are in Python 3, so it may change its different from python 2 or upgraded version
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.