Python List append() Method


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.

Syntax of append() 🔝

my_list.append(item)

The append() method accepts one argument. The supplied item is added after the current last element of the list.

Basic append() Example 🔝

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.

Appending a Number

numbers=[10, 20, 30]

numbers.append(40)

print(numbers)
Output
[10, 20, 30, 40]

Adding Items to an Empty List 🔝

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.

Return Value of append() 🔝

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']

Do Not Assign the Result of append() Back to the List

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.

append() Accepts Only One Argument 🔝

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.

Appending Another List as One Item 🔝

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.

Difference Between append() and extend() 🔝

The append() method adds its argument as one item. The extend() method adds each item from an iterable separately.

Using append()

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']]

Using extend()

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.

Difference Between append() and insert() 🔝

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.

Using insert() at the End of a List

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

Checking List Length Before and After append() 🔝

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']

Using append() Inside a Loop 🔝

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.

Creating a List from JSON Data Using append() 🔝

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.

Summary of append() 🔝

  • append() adds one item to the end of a list.
  • It accepts exactly one argument.
  • The supplied object is added as one list element.
  • It modifies the existing list in place.
  • Its return value is None.
  • It is commonly used to build lists inside loops.
  • Use extend() when individual elements of another iterable should be added separately.
  • Use insert() when an item must be added at a specific position.
All List Methods extend() insert() Questions with Solutions on List




Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2026   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer