Python Program to Append an Item to a List

Write a Python program to append an item to a list. In Python, we have multiple options to append or add an item to a list, and they are the append(), insert(), and extend() functions. This page shows all the three possibilities with an example of each.

Using append() function

In this example, we use the list append() method on the pre declared list. The list append method adds a new element to the end of a list. The syntax of the append() function is list.append(item)

numbers = [10, 20, 30, 40, 50]

print("Numeric List Before Appending Item")
print(numbers)

numbers.append(65)

print("Numeric List After Appending First Item")
print(numbers)

value = int(input("Please enter the List Item = "))
numbers.append(value)

print("Numeric List After Appending Second Item")
print(numbers)
Python Program to Append an Item to a List

Using insert() function

This python program appends an item to a list using the index function. The list index method adds a list element at the specified index. The syntax of the insert() function is list.insert(index, item)

countries = ["India", "USA", "UK", "Italy"]

print("List Before Appending Item")
print(countries)

countries.insert(3, "Japan")
print("\nList After Appending Japan at 3rd Index Position")
print(countries)

countries.insert(0, "China")
print("\nList After Appending China at 0 Index Position")
print(countries)

countries.insert(6, "France")
print("\nList After Appending France at 6 Index Position")
print(countries)
List Before Appending Item
['India', 'USA', 'UK', 'Italy']

List After Appending Japan at 3rd Index Position
['India', 'USA', 'UK', 'Japan', 'Italy']

List After Appending China at 0 Index Position
['China', 'India', 'USA', 'UK', 'Japan', 'Italy']

List After Appending France at 6 Index Position
['China', 'India', 'USA', 'UK', 'Japan', 'Italy', 'France']

This example allows entering the list index and the list value and adds that new item using the index method.

numbers = [11, 22, 33, 44, 55, 66]

print("Numeric List Before Inserting Item")
print(numbers)


index = int(input("Please enter Index Position = "))
value = int(input("Please enter the List Value = "))

numbers.insert(index, value)

print("Numeric List After Appending Item")
print(numbers)
Numeric List Before Inserting Item
[11, 22, 33, 44, 55, 66]
Please enter Index Position = 4
Please enter the List Value = 111
Numeric List After Appending Item
[11, 22, 33, 44, 111, 55, 66]

Python program to append an item to a list using extend method

The list extend method adds any iterable such as list, tuple, string, etc to the end of the existing list. The syntax of the extend() function is list.extend(iterator)

countries = ["India", "USA", "UK", "Italy"]

print("List Before Appending Item")
print(countries)

tuple1 = ("Japan", "China", "France")
countries.extend(tuple1)
print("\nList After Appending Tuple using extend")
print(countries)

list1 = [19, 17, 39, 55]
countries.extend(list1)
print("\nList After Appending List sing extend")
print(countries)

countries.extend((11.5, 19.2))
print("\nList After Appending 11.5, 19.2 using extend")
print(countries)
Python program to append an item to a list using extend function