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 here, we use the append method. The list append method adds a new element to the end of a list.

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

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.

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 Python 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.

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)
List Before Appending Item
['India', 'USA', 'UK', 'Italy']

List After Appending Tuple using extend
['India', 'USA', 'UK', 'Italy', 'Japan', 'China', 'France']

List After Appending List sing extend
['India', 'USA', 'UK', 'Italy', 'Japan', 'China', 'France', 19, 17, 39, 55]

List After Appending 11.5, 19.2 using extend
['India', 'USA', 'UK', 'Italy', 'Japan', 'China', 'France', 19, 17, 39, 55, 11.5, 19.2]

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.