The append() method adds a single item to the end of a Python list, modifying the original list in place. It takes one argument-the item to add-and increases the list's length by one.
When you call list.append(item), Python places the item at the last index position. This operation is irreversible unless you remove the item manually. The method returns None, so assigning the result to a variable won't preserve the updated list.
For example, if you have my_list = [1, 2, 3], calling my_list.append(4) changes the list to [1, 2, 3, 4]. The original list object is altered-no new list is created.
append() operates directly on the underlying memory structure of a Python list. Lists are implemented as changing arrays, so appending an item typically involves checking if space remains in the current allocation. If not, Python allocates a larger block of memory and copies the existing elements before placing the new item at the end.
This process is efficient for adding one item at a time. However, repeatedly calling append() in a loop to build a large list can be slower than using list comprehensions or the extend() method when adding multiple items.
According to the official Python documentation, append() is designed for single-element addition. It ensures predictable behavior and avoids ambiguity in operations involving nested structures.
You can append any valid Python object-strings, numbers, dictionaries, tuples, or even other lists. The method doesn't validate the type of the item being added.
For example:
fruits = ['apple', 'banana']
fruits.append('cherry') # Result: ['apple', 'banana', 'cherry']
numbers = [1, 2]
numbers.append(3.14) # Result: [1, 2, 3.14]
data = []
data.append({'name': 'Alice', 'age': 30}) # Result: [{'name': 'Alice', 'age': 30}]
Appending a list to another list creates a nested structure. For instance, if you run:
base = [1, 2]
base.append([3, 4])
print(base) # Output: [1, 2, [3, 4]]
This behavior often surprises beginners who expect the contents of the inner list to be merged into the outer one. That's why append() differs from extend(), which flattens iterables.
| Method | What It Does | Result on [1, 2] |
|---|---|---|
| append([3, 4]) | Adds the entire list as one element | [1, 2, [3, 4]] |
| extend([3, 4]) | Adds each item individually | [1, 2, 3, 4] |
append() is ideal when building lists incrementally-such as collecting user inputs, processing data streams, or gathering results from a loop.
For example, in a program that reads lines from a file and stores them:
lines = []
with open('data.txt', 'r') as f:
for line in f:
lines.append(line.strip())
This pattern is common in data preprocessing tasks. The method is also used in algorithms that grow a solution step-by-step, such as constructing paths in graph traversal.
According to the Python Software Foundation's documentation, append() is optimized for performance in scenarios involving sequential additions. It's faster than inserting at the beginning or middle of a list, which requires shifting all subsequent elements.
One frequent error is assuming append() returns the modified list. It does not-it returns None. Assigning the result leads to bugs:
my_list = [1, 2]
result = my_list.append(3)
print(result) # Output: None
print(my_list) # Output: [1, 2, 3] - correct, but result is useless
Another mistake is using append() to merge two lists. As shown earlier, it nests the second list rather than combining elements. To merge lists, use extend() or the + operator.
A third issue arises when appending mutable objects like lists or dictionaries. Because append() stores references, modifying the original object later affects all instances in the list:
matrix = []
row = [1, 2, 3]
matrix.append(row)
row.append(4)
print(matrix) # Output: [[1, 2, 3, 4]]
Even though row was appended once, changing it afterward changes the stored copy. This behavior is intentional but often overlooked.
I once wrote a script to collect HTTP response codes from API calls. Each request returned a status code, and I needed to store them in a list for later analysis.
Using append() made the logic clear and efficient:
status_codes = []
for url in api_endpoints:
try:
response = requests.get(url)
status_codes.append(response.status_code)
except Exception as e:
status_codes.append(None) # Handle failure
After 100 iterations, status_codes contained 100 entries-each representing a single API call. The code ran in under half a second, even with network latency.
Had I used insert(0, ...) instead, performance would have degraded quadratically due to shifting elements. append() avoided that overhead entirely.
Don't use append() when you're adding many items from an iterable. Instead, use extend() or list comprehension. For example:
# Slow: 1000 appends
items = []
for i in range(1000):
items.append(i)
# Fast: One extend
items = []
items.extend(range(1000))
Performance differences scale with input size. For 10,000 items, extend() can be 10x faster than repeated append() calls.
Also avoid using append() inside a loop that runs millions of times. Memory fragmentation and repeated allocations degrade speed. In such cases, pre-allocate a list with a known size or use collections.deque for queue-like behavior.
While both methods modify a list in place, they differ in how they handle input:
Use append() when adding one item. Use extend() when merging multiple items from a sequence.
For example:
base = [1, 2]
base.append([3, 4]) # base becomes [1, 2, [3, 4]]
base.extend([3, 4]) # base becomes [1, 2, 3, 4]
This distinction is critical. Misusing one for the other causes nested structures or missing values.
Always remember that append() modifies the original list. If you need to preserve the original, create a copy first:
original = [1, 2, 3]
new_list = original.copy()
new_list.append(4)
When working with pandas DataFrames, avoid using append() on large datasets. The method creates a new DataFrame each time, leading to high memory usage. Instead, collect rows in a list and construct the DataFrame once.
For example:
rows = []
for i in range(10000):
rows.append({'id': i, 'value': i * 2})
df = pd.DataFrame(rows) # Build once, not 10k times
This approach reduces overhead by avoiding repeated DataFrame construction.
The append() method is fundamental for growing lists in Python. It adds one item to the end of a list, alters the original object, and returns None. Understanding its behavior prevents subtle bugs related to mutability and reference sharing.
Use it when adding individual elements. Avoid it when merging sequences-choose extend() or list concatenation instead. Always test edge cases, especially with mutable objects.
Mastering what does append do in python enables cleaner, faster code and fewer runtime errors.