Last modified: Mar 25, 2026 By Alexander Williams

Python Array Append: How to Add Elements

Adding items to a collection is a common task. In Python, the primary tool for this is the append() method. It is simple and powerful.

This guide explains how to use append effectively. We will cover lists, which are Python's built-in arrays. We will also touch on the array module.

What is the Append Method?

The append() method adds a single element to the end of a list. It modifies the original list directly. This is called an in-place operation.

It does not create a new list. This makes it very efficient for building collections step-by-step. The syntax is straightforward.


# Basic syntax of the append method
my_list.append(element)
    

Basic Example of Using Append

Let's start with a simple example. We will create an empty list and add numbers to it.


# Create an empty list
numbers = []

# Append elements one by one
numbers.append(10)
numbers.append(20)
numbers.append(30)

print("The list is:", numbers)
    

The list is: [10, 20, 30]
    

The list grows with each call to append(). The new element is always placed at the end. This preserves the order of insertion.

Appending Different Data Types

Python lists can hold mixed data types. You can append integers, strings, floats, or even other lists.


mixed_list = [1, "hello"]

# Append a float
mixed_list.append(3.14)
# Append another list (this creates a nested list)
mixed_list.append([4, 5])

print("Mixed list:", mixed_list)
    

Mixed list: [1, 'hello', 3.14, [4, 5]]
    

Notice that appending a list creates a single element inside the main list. It does not merge the lists. To merge lists, you would use extend() or the + operator.

Understanding the difference between append and extend is crucial for managing your Python array size effectively.

Append vs. Extend vs. Insert

Python offers other methods to add elements. Knowing when to use each is key.

  • append(x): Adds one element x to the end.
  • extend(iterable): Adds all elements from an iterable (like another list) to the end.
  • insert(i, x): Inserts element x at a specific index i.

list_a = [1, 2]

# Append adds the whole list as one item
list_a.append([3, 4])
print("After append:", list_a)  # Output: [1, 2, [3, 4]]

list_b = [1, 2]
# Extend adds each element from the iterable
list_b.extend([3, 4])
print("After extend:", list_b)  # Output: [1, 2, 3, 4]

list_c = [1, 3]
# Insert places an element at a specific position
list_c.insert(1, 2)  # Insert 2 at index 1
print("After insert:", list_c)  # Output: [1, 2, 3]
    

Using Append in Loops

A common pattern is to build a list inside a loop. You start with an empty list. Then you append results from each iteration.


# Create a list of squares from 0 to 4
squares = []
for i in range(5):
    square = i * i
    squares.append(square)

print("Squares:", squares)
    

Squares: [0, 1, 4, 9, 16]
    

This is a fundamental technique in Python. It is used in data processing, file reading, and more. For a deeper dive into structuring such data, see our Python Array Implementation Guide.

Append with the Array Module

Python has a dedicated array module. It provides a more memory-efficient array for uniform numeric types. It also has an append() method.


import array

# Create an array of integers ('i' is the type code)
int_array = array.array('i', [1, 2, 3])

# Append a new integer
int_array.append(4)

print("Integer array:", int_array)
    

Integer array: array('i', [1, 2, 3, 4])
    

The array module is stricter than a list. You can only append elements of the same type. This constraint saves memory.

Performance and Time Complexity

The append() method is highly optimized. Its average time complexity is O(1). This means it takes constant time, regardless of the list's Python array length.

Python lists are implemented as dynamic arrays. They occasionally need to resize when full. The amortized cost of append() remains low. It is the preferred way to add single items.

Common Mistakes and Best Practices

Beginners often make a few common errors. Let's address them.

Mistake 1: Forgetting that append() returns None. It modifies the list in place.


my_list = [1, 2]
# Wrong: Trying to assign the result
new_list = my_list.append(3)  # new_list will be None!
print("my_list is:", my_list)
print("new_list is:", new_list)
    

my_list is: [1, 2, 3]
new_list is: None
    

Best Practice: Call append() on its own line. Do not assign its result.

Mistake 2: Using append() when you mean extend(). This creates nested lists unintentionally.

Best Practice: Be clear about your goal. Add one item? Use append(). Add multiple items from another list? Use extend().

Conclusion

The append() method is a cornerstone of Python programming. It is the standard way to add a single element to the end of a list or array.

Remember its key traits. It modifies the list in place. It returns None. It is fast and efficient.

Combine it with loops to build dynamic data structures. Use the array module for numeric efficiency. Now you can confidently use append() in your projects.