Add List Items
Addition Methods
Lists provide several ways to add elements. The append() method adds a single item at the end. The extend() method adds all items from another iterable. The insert() method adds an item at a specified position. You can also use concatenation with + to create a new list or += to extend the existing list.
fruits = ['apple']
# Append single item
fruits.append('banana')
# Extend with multiple items
fruits.extend(['cherry', 'date'])
# Insert at position
fruits.insert(1, 'avocado')
# Concatenation
new_fruits = fruits + ['elderberry', 'fig']
print(fruits)
print(new_fruits)
['apple', 'avocado', 'banana', 'cherry', 'date'] ['apple', 'avocado', 'banana', 'cherry', 'date', 'elderberry', 'fig']
Performance Considerations
Not all addition methods perform equally. append() is generally very efficient (amortized O(1)), while inserting at the beginning of a list requires shifting all elements and is O(n). Concatenation with + creates a new list, which can be less efficient if repeated often.
from timeit import timeit
# Compare common operations
print("append:", timeit('lst.append(1)', 'lst = []'))
print("insert(0):", timeit('lst.insert(0, 1)', 'lst = []'))
print("concatenation:", timeit('lst + [1]', 'lst = []'))
Bulk Addition Patterns
When adding multiple items at once, you can use extend(), +=, or unpacking syntax. These approaches are flexible and efficient for combining lists or adding generated values.
# Adding from another list
main = [1, 2, 3]
main += [4, 5, 6] # In-place extension
# Adding from generator
def squares(n):
for i in range(n):
yield i**2
numbers = [1, 2, 3]
numbers.extend(squares(3)) # [1, 2, 3, 0, 1, 4]
# Adding with unpacking
parts = ['head', 'shoulders']
full = [*parts, 'knees', 'toes']
[1, 2, 3, 4, 5, 6] [1, 2, 3, 0, 1, 4] ['head', 'shoulders', 'knees', 'toes']