How can I get the concatenation of two lists in Python without modifying either one?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Concatenating two lists in Python without modifying either original list means creating a new list that contains elements from both. The simplest approach is the + operator (list1 + list2), which always returns a new list. Other options include unpacking with [*list1, *list2], itertools.chain, and list comprehensions. Each has different performance characteristics depending on whether you need a concrete list or just need to iterate over the combined elements.
Method 1: The + Operator
The + operator creates a new list by copying all elements from both lists. Neither a nor b is modified. This is the most readable and commonly used approach.
Method 2: Unpacking with *
The * unpacking syntax (Python 3.5+) expands each list inside a new list literal. This also works with more than two lists and with other iterables:
Method 3: itertools.chain (Lazy)
chain does not create an intermediate list. It yields elements from the first iterable, then from the second. This is memory-efficient when you only need to iterate once over very large lists.
Method 4: List Comprehension
This is more verbose than + for simple concatenation but useful when you need to transform or filter elements during concatenation:
Method 5: copy + extend
This is equivalent to a + b but expressed as two steps. It is slightly less readable but useful when building up a list incrementally.
Methods That DO Modify the Original
Be careful with these — they change the first list:
If you want a new list without modifying either original, avoid extend() and += on the original variables.
Performance Comparison
The + operator and [*a, *b] are the fastest for creating a concrete list. chain is fastest when you only need to iterate (no list creation). Comprehensions are slowest due to Python-level loop overhead.
Concatenating Multiple Lists
Avoid sum(lists, []) for large lists. It creates a new list at each step, making it O(n^2). Use chain.from_iterable instead.
Common Pitfalls
- Using
extend()or+=when you want a new list: Both modify the first list in place. Use+or[*a, *b]to create a new list without side effects. - Using
append()instead ofextend()or+:a.append(b)addsbas a single nested element[1, 2, 3, [4, 5, 6]], not as individual elements. - Using
sum(lists, [])for many lists: This is O(n^2) because each+creates a new intermediate list. Useitertools.chain.from_iterablefor O(n) concatenation of many lists. - Assuming shallow copy is deep copy:
a + bcreates a new list, but the elements themselves are not copied. If elements are mutable (like nested lists), modifying them affects both the original and the concatenated list. - Forgetting that
chainreturns an iterator:chain(a, b)can only be iterated once. If you need to use the result multiple times, convert to a list withlist(chain(a, b)).
Summary
- Use
a + bfor the simplest, most readable concatenation that returns a new list - Use
[*a, *b]for concatenating multiple iterables of different types - Use
itertools.chain(a, b)for memory-efficient lazy iteration without creating a new list - Avoid
extend()and+=when you need to preserve the original lists - For concatenating many lists, use
chain.from_iterableinstead ofsum(lists, [])

