Adding lists together in Python is a common task when you need to combine multiple sequences into one larger collection. This process, often called concatenation, can be done in several ways depending on your data structure and performance needs.
Python provides clear, readable syntax for merging lists that keeps your code straightforward and easy to maintain. Below you will find practical methods and examples to help you combine lists efficiently.
| Method | Syntax | Modifies Original | Use Case |
|---|---|---|---|
| Plus operator | list1 + list2 | No | Simple merge, creates new list |
| Extend method | list1.extend(list2) | Yes | In-place addition, memory efficient |
| List unpacking | [*list1, *list2] | No | Readable, works with multiple lists |
| Chain from itertools | list(chain(list1, list2)) | No | Lazy evaluation, good for iterators |
Using the Plus Operator for Concatenation
Basic Addition with +
The plus operator creates a new list by joining the elements of two or more lists. This approach is intuitive and works well for simple cases where you do not want to change the original lists.
Combining More Than Two Lists
You can chain multiple plus operators to merge several lists in a single line. While readable for a small number of lists, long chains can become harder to scan quickly.
Extending Lists In-Place
How extend Works
The extend method adds all elements from another list to the end of the original list, modifying it directly. This avoids creating a new list and can be faster when working with large data.
Performance Considerations
Because extend changes the list in place, it uses less extra memory compared to the plus operator. Use extend when you no longer need to keep the original list unchanged.
List Unpacking and Chain Methods
Unpacking with the Star Operator
List unpacking using * provides a compact and readable way to combine lists. It is especially useful when you need to merge lists inside a new literal without calling methods.
Using itertools.chain
The chain function from the itertools module allows lazy concatenation of multiple iterables. Convert the result to a list only when you need materialized data, which can improve performance in pipelines.
Best Practices and Recommendations
- Use the plus operator when you need a new list and want to keep originals unchanged.
- Choose extend for in-place updates and better memory efficiency with large lists.
- Prefer list unpacking for clear, concise code when merging a small number of lists.
- Consider itertools.chain for lazy evaluation and working with multiple iterators.
FAQ
Reader questions
Does the plus operator change the original lists?
No, the plus operator creates a new list and leaves the original lists untouched.
Can I add tuples or other sequences to a list using these methods?
Yes, you can concatenate lists with tuples and other iterables by converting them to lists first or using unpacking.
Is extend faster than the plus operator for large lists?
Generally, extend is faster and uses less memory because it modifies the list in place instead of building a new one.
Can I merge more than two lists with unpacking in a single line?
Yes, you can include as many starred lists as you need inside a new list literal with unpacking.