The Python append() method adds one item to the end of a
list. The item can be a string, number, list, tuple, dictionary, or another Python object.
The append() method changes the existing list and adds the supplied object as a single new element.
my_list.append(item)
The append() method accepts one argument. The supplied item is added after the current last element of the list.
my_list=['Alex', 'Ronald', 'John']
my_list.append('Ravi')
print(my_list)
Output
['Alex', 'Ronald', 'John', 'Ravi']
The value 'Ravi' is added after the existing last item of the list.
numbers=[10, 20, 30]
numbers.append(40)
print(numbers)
Output
[10, 20, 30, 40]
We can start with an empty list and add items one by one using append().
my_list=[]
my_list.append('Alex')
my_list.append('Ronald')
my_list.append('John')
print(my_list)
Output
['Alex', 'Ronald', 'John']
This pattern is useful when values are collected gradually while a program is running.
The append() method modifies the existing list in place and returns None.
my_list=['Alex', 'Ronald']
result=my_list.append('Ravi')
print(result)
print(my_list)
Output
None
['Alex', 'Ronald', 'Ravi']
Since append() returns None, assigning its return value back to the list variable replaces the list with None.
my_list=['Alex', 'Ronald']
my_list=my_list.append('Ravi')
print(my_list)
Output
None
Use append() directly on the list without assigning its return value back to the same variable.
The append() method accepts exactly one item at a time. Passing two separate arguments causes a TypeError.
my_list=[10, 20]
my_list.append(30, 40)
Sample Error
TypeError: list.append() takes exactly one argument
To add several values separately, call append() more than once or use the
extend() method.
A complete list can also be passed to append(). In this case, the second list becomes one new element inside the first list.
my_list=['Alex', 'Ronald', 'John']
my_list2=['Ravi', 'John', 'Ronald']
my_list.append(my_list2)
print(my_list)
Output
['Alex', 'Ronald', 'John', ['Ravi', 'John', 'Ronald']]
Here, my_list2 is added as one item. Its individual elements are not added separately.
The append() method adds its argument as one item. The
extend() method adds each item from an iterable separately.
my_list=['Alex', 'Ronald', 'John']
my_list2=['Ravi', 'John', 'Ronald']
my_list.append(my_list2)
print(my_list)
Output
['Alex', 'Ronald', 'John', ['Ravi', 'John', 'Ronald']]
my_list=['Alex', 'Ronald', 'John']
my_list2=['Ravi', 'John', 'Ronald']
my_list.extend(my_list2)
print(my_list)
Output
['Alex', 'Ronald', 'John', 'Ravi', 'John', 'Ronald']
Use append() when the new object should become one list item. Use
extend() when the individual elements of another iterable should be added separately.
The append() method always adds an item at the end of a list.
The insert() method can add an item at a specified index.
my_list=['Alex', 'Ronald', 'John']
my_list.insert(1, 'Ravi')
print(my_list)
Output
['Alex', 'Ravi', 'Ronald', 'John']
Here, 'Ravi' is inserted at index 1. With append(), the same value would be placed at the end.
We can use the len() function with
insert() to add an item after the current last element.
my_list=['Alex', 'Ronald', 'John']
# Same final position as append()
my_list.insert(len(my_list), 'new name')
print(my_list)
Output
['Alex', 'Ronald', 'John', 'new name']
When an item only needs to be added to the end, append() is simpler than using insert(len(my_list), item).
The len() function returns the number of elements in a list. Adding one item with append() increases the length by one.
my_list=['Alex', 'Ronald', 'John']
print("Length before append:", len(my_list))
my_list.append('Ravi')
print("Length after append:", len(my_list))
print(my_list)
Output
Length before append: 3
Length after append: 4
['Alex', 'Ronald', 'John', 'Ravi']
The append() method is often used inside a
for loop to build a new list from generated or processed values.
numbers=[]
for i in range(1, 6):
numbers.append(i * 2)
print(numbers)
Output
[2, 4, 6, 8, 10]
The range() function supplies values from 1 through 5. Each value is multiplied by 2, and append() adds the result to numbers.
We can read records from a JSON file and use append() to collect selected values in a list.
Here is the sample JSON file used in this example.
import json
path=r"D:\my_data\student.json"
with open(path) as fob:
data=json.load(fob)
names=[]
for student in data:
names.append(student['name'])
print(names)
The json.load() function reads the JSON data.
The loop reads the name value from each student record, and append() adds that value to the names list.
Using with open() closes the file automatically after it has been read.
append() adds one item to the end of a list.None.extend() when individual elements of another iterable should be added separately.insert() when an item must be added at a specific position.Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.