The append() method adds a single item to the end of a Python list. It modifies the original list in place and takes one argument: the item to add.
When you call list.append(item), Python inserts item as the last element. The method returns None, meaning it doesn't produce a new list-it changes the existing one. This behavior is critical for understanding how it differs from other list operations.
For example, if you have fruits = ['apple', 'banana'] and run fruits.append('orange'), the list becomes ['apple', 'banana', 'orange']. The original list object is altered; no new list is created.
append() accepts any Python object as input-an integer, string, another list, even a custom class instance. The method appends the entire object as a single element, not its contents.
If you pass a list to append(), it adds that list as one item. For instance:
numbers = [1, 2]
numbers.append([3, 4])
print(numbers) # Output: [1, 2, [3, 4]]
The result is a nested list. The inner list [3, 4] is not expanded-it's treated as a single element.
This behavior contrasts sharply with extend(), which iterates through the input and adds each individual item. Using extend() on [3, 4] would yield [1, 2, 3, 4].
Use append() when adding one item at a time-like logging events, building a changing list of user inputs, or collecting results from a loop.
Common scenarios include:
It's ideal for sequential data collection where each item stands alone.
Developers often confuse append() with extend(). The key difference lies in how they handle sequences:
| Method | Input Type | Result |
|---|---|---|
append() |
List | Adds the entire list as one element |
extend() |
List | Adds each item individually |
For example:
colors = ['red']
colors.append(['blue', 'green'])
print(colors) # ['red', ['blue', 'green']]
colors = ['red']
colors.extend(['blue', 'green'])
print(colors) # ['red', 'blue', 'green']
Another frequent error is assuming append() returns a new list. It does not. The method returns None, so assigning it to a variable leads to bugs:
items = []
new_items = items.append('new') # new_items is None
print(new_items) # None
Always use append() directly on the list object, not as part of an assignment.
Suppose you're writing a script that processes tasks from a configuration file. You read each task and add it to a list for later execution.
Here's a working example:
tasks = []
# Simulate reading tasks from a config
config_tasks = ['send_email', 'backup_data', 'run_tests']
for task in config_tasks:
tasks.append(task)
print(tasks)
# Output: ['send_email', 'backup_data', 'run_tests']
This pattern ensures each task is added as a separate entry. If you used extend() instead, you'd still get the same result-but only because the input is a list of strings. The method choice matters when dealing with nested structures.
I've used append() in production scripts to collect error logs during batch processing. Each time a step fails, I append a dictionary containing timestamp, error code, and message. The list grows safely, and I can later export it to JSON for analysis.
Understanding append list python requires knowing how lists are structured in memory. Lists are mutable sequences stored as arrays of references. When you call append(), Python increases the range size if needed and stores a reference to the new item.
To start with a list, see how to create a list in Python. Once initialized, append() lets you grow it dynamically.
Always test your logic with edge cases-empty lists, duplicates, or objects that don't support comparison. These can expose subtle bugs when using append() in complex workflows.
Use append() when you want to add one item to a list. Use extend() when you want to merge lists or add multiple values at once. Choose based on expanding a sequence.