Combining Lists Non-Destructively
In Python, lists are fundamental data structures used to store collections of items. Often, you’ll need to combine the contents of two or more lists into a single list. A common requirement is to do this without altering the original lists – that is, creating a new list containing all elements from both, leaving the originals untouched. This tutorial explains several methods to achieve this.
The ‘+’ Operator: The Simplest Approach
The most straightforward way to concatenate lists in Python without modification is using the +
operator. This operator creates a new list containing all the elements of the first list followed by all the elements of the second list.
list1 = [1, 2, 3]
list2 = [4, 5, 6]
combined_list = list1 + list2
print(combined_list) # Output: [1, 2, 3, 4, 5, 6]
print(list1) # Output: [1, 2, 3] (original list unchanged)
print(list2) # Output: [4, 5, 6] (original list unchanged)
As you can see, list1
and list2
remain unchanged, while combined_list
holds the concatenated result. This method is concise and easy to read, making it ideal for most common scenarios.
Using itertools.chain()
for Memory Efficiency
When dealing with very large lists, creating a new list with the +
operator might consume significant memory. In such cases, itertools.chain()
provides a more memory-efficient solution.
itertools.chain()
doesn’t immediately create a new list. Instead, it returns an iterator that yields elements from each input list sequentially. This means elements are processed on demand, avoiding the need to store the entire concatenated list in memory at once.
import itertools
list1 = [1, 2, 3]
list2 = [4, 5, 6]
combined_iterator = itertools.chain(list1, list2)
# To get a list from the iterator:
combined_list = list(combined_iterator)
print(combined_list) # Output: [1, 2, 3, 4, 5, 6]
#You can also iterate directly:
for item in itertools.chain(list1, list2):
print(item)
Note that the combined_iterator
is a one-time use iterator. Once you’ve iterated through it, you’ll need to create a new one if you want to access the combined elements again. Converting it into a list using list()
creates a new list in memory, so this approach is best if you need to iterate through the combined elements only once.
Using sum()
with an Initial Value
The sum()
function, typically used for numerical summation, can also concatenate lists if provided with a suitable starting value. This works by repeatedly adding lists together, starting with the initial value.
list1 = [1, 2, 3]
list2 = [4, 5, 6]
combined_list = sum([list1, list2], [])
print(combined_list) # Output: [1, 2, 3, 4, 5, 6]
Here, []
is the initial value – an empty list. sum()
then adds list1
and list2
to this empty list, creating the concatenated result. Be cautious when using sum()
with non-list types, as it may raise a TypeError
. Also note that using sum
for list concatenation is generally less readable than using the +
operator or itertools.chain
.
Choosing the Right Approach
- For most general cases, the
+
operator offers the simplest and most readable solution. - If memory efficiency is a crucial concern (e.g., dealing with very large lists),
itertools.chain()
is the preferred option. sum()
can be used, but it’s less idiomatic and potentially less readable for list concatenation.