Python append List is one of the List functions used to add an item to the end of an old list. In this section, we discuss how to use this append function on the list with practical examples.
The syntax of this Python append List function is:
list.append(New_item)
This list append method help us to add given item (New_item) at the end of the Old_list
Python append List Function Example
First, we declared an integer list with four integer values. Next list append code adds 50 to the end of List a
TIP: Please refer to List and List functions articles to understand everything about Lists in Python.
# Python Append List Items a = [10, 20, 30, 40] print("Original List Items are : ", a) a.append(50) print("List Items are : ", a)
OUTPUT
append List Function Example 2
In this program, we are adding three integer values to an integer list using this list append function.
# Python Append List Items a = [10, 20, 30, 40] print("Original List Items are : ", a) a.append(50) a.append(60) a.append(70) print("List Items are : ", a)
OUTPUT
Python List append Example 3
This example shows you the working functionality of the list append function on a string list.
In this example, we declared a string list. Next, we used the list append function to add another fruit to this list.
# Python Append List Items Fruits = ['Apple', 'Orange', 'Kiwi', 'Grape'] print("Original List Items are : ", Fruits) Fruits.append('Banana') print("List Items are : ", Fruits)
OUTPUT
List append Function Example 4
This program is the same as the first list append example. However, this time, we are allowing the user to enter the length of a List. Next, we used For Loop to append those numbers to the list.
# Python Append List Items intList = [] number = int(input("Please enter the Total Number of List Elements: ")) for i in range(1, number + 1): value = int(input("Please enter the Value of %d Element : " %i)) intList.append(value) item = int(input("Please enter the New Item to append : " )) print("Original List Items are : ", intList) intList.append(item) print("New List Items are : ", intList)
OUTPUT
List append Function Example 5
In this program, we used the list append function to add words to a list
# Python Append List Items strList = [] number = int(input("Please enter the Total Number of List Elements: ")) for i in range(1, number + 1): value = input("Please enter the Value of %d Element : " %i) strList.append(value) item = input("Please enter the New Item to append : " ) print("Original List Items are : ", strList) strList.append(item) print("New List Items are : ", strList)
OUTPUT