Take the content of a list and append it to another list
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Python, adding one list to another can mean either appending the second list as a single nested element or extending the first list with each element from the second. Most confusion comes from using append when extend or += was actually intended.
append and extend Do Different Things
append adds one object to the target list. If that object is itself a list, the result becomes nested.
The output is:
That is correct when you want the entire second list to remain one item.
extend does something different. It iterates over the incoming list and adds each element individually.
Now the result is flat:
When people say “append the contents of one list to another,” extend is usually what they mean.
+= Is an In-Place Extension
The += operator behaves similarly to extend for lists.
This mutates the existing list instead of creating a new one. That is concise and often efficient, but it matters when other variables refer to the same list.
Because alias points at the same list object, it also sees the change.
Make a New Combined List When Mutation Is Undesirable
If you want to preserve both inputs unchanged, create a new list.
Sequence unpacking is another readable option.
This style is useful in codebases that favor explicit non-mutating data transformations.
extend Works with Any Iterable
The argument to extend does not have to be a list. Any iterable works.
That flexibility is convenient, but be careful with iterables such as strings because they extend one element at a time.
The result is ['a', 'b', 'c'], not ['a', 'bc'].
Think About Nested Mutable Objects
List merging is shallow. It copies references to contained objects, not deep independent copies.
The first nested list changes in both places because both lists reference the same inner object. If isolation matters, you need deeper copying logic rather than a different append technique.
Common Pitfalls
- Using
appendwhen the intended result is a flat list. - Forgetting that
+=mutates the existing list in place. - Assuming
extendonly accepts lists when it actually accepts any iterable. - Extending with a string and being surprised that each character is added separately.
- Expecting merged nested objects to be deep-copied automatically.
Summary
- Use
appendto add one object, including an entire list as a nested item. - Use
extendor+=to add elements from one list into another. - Use
+or unpacking when you want a new combined list instead of mutation. - Remember that
extendaccepts any iterable. - List merging is shallow, so nested mutable objects remain shared unless you copy them separately.

