Skip to content

How to Add Multiple Values to Python Lists

Learn the correct ways to add multiple items to a Python list, compare extend(), concatenation (+), and unpacking (*) to avoid nested lists and improve performance.

Python lists are mutable sequences, meaning you can modify them after creation. While the append() method is well-known for adding a single element, attempting to use it for multiple values often leads to unexpected results (like nested lists).

This guide explains the most efficient ways to add multiple items to a list, covering the standard extend() method, operator concatenation, and unpacking techniques.

The most "Pythonic" and efficient way to add multiple elements to an existing list is the .extend() method. It takes an iterable (like another list, tuple, or set) and appends each of its elements to the original list.

Key Characteristic: Modifies the list in-place (does not create a new list object).

fruits = ['apple', 'banana']
new_items = ['cherry', 'durian', 'elderberry']

fruits.extend(new_items)

print(f"Updated list: {fruits}")

Output:

Updated list: ['apple', 'banana', 'cherry', 'durian', 'elderberry']

Method 2: Using the + Operator (Concatenation)

If you want to combine two lists but keep the original list unchanged, use the + operator. This creates a new list containing elements from both.

Key Characteristic: Creates a new list object in memory.

original_list = [1, 2, 3]
additions = [4, 5]

combined = original_list + additions

print(f"Original: {original_list}")
print(f"Combined: {combined}")

Output:

Original: [1, 2, 3]
Combined: [1, 2, 3, 4, 5]

Method 3: List Unpacking (Python 3.5+)

For a more modern syntax, especially when initializing a new list from several sources, you can use the asterisk * operator to unpack elements.

list_a = [10, 20]
list_b = [30, 40]

merged_list = [*list_a, *list_b, 50]

print(f"Merged: {merged_list}")

Output:

Merged: [10, 20, 30, 40, 50]

Common Mistake: Appending a List

A frequent error is using .append() when you actually intend to use .extend(). The append() method adds its argument as a single element. If you pass a list to append(), you get a list inside a list (nested list).

numbers = [1, 2, 3]
more_numbers = [4, 5]

try:
    numbers.append(more_numbers)
    print(f"Result with append: {numbers}")
    print(f"Length: {len(numbers)}") # Length is 4, not 5
except Exception as e:
    print(e)

Output:

Result with append: [1, 2, 3, [4, 5]]
Length: 4

Conclusion

To add multiple values to a Python list:

  1. Use list.extend(iterable) (or list += iterable) to modify the list in-place. This is usually the most memory-efficient method.
  2. Use new_list = list_a + list_b if you need to preserve the original lists and create a new one.
  3. Avoid list.append(other_list) unless your specific goal is to create a nested list structure.
Navigation