Python
list-manipulation
programming
data-structures
coding

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.

python
1left = [1, 2]
2right = [3, 4]
3
4result = left.copy()
5result.append(right)
6print(result)

The output is:

text
[1, 2, [3, 4]]

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.

python
1left = [1, 2]
2right = [3, 4]
3
4result = left.copy()
5result.extend(right)
6print(result)

Now the result is flat:

text
[1, 2, 3, 4]

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.

python
letters = ["a", "b"]
letters += ["c", "d"]
print(letters)

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.

python
1base = [1, 2]
2alias = base
3base += [3]
4print(alias)

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.

python
1left = [10, 20]
2right = [30, 40]
3
4merged = left + right
5print(merged)
6print(left)
7print(right)

Sequence unpacking is another readable option.

python
merged = [*left, *right]
print(merged)

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.

python
1values = [1, 2]
2values.extend((3, 4))
3values.extend(n for n in range(5, 7))
4print(values)

That flexibility is convenient, but be careful with iterables such as strings because they extend one element at a time.

python
chars = ["a"]
chars.extend("bc")
print(chars)

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.

python
1original = [[1], [2]]
2merged = original.copy()
3merged.extend([[3]])
4merged[0].append(99)
5
6print(original)
7print(merged)

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 append when the intended result is a flat list.
  • Forgetting that += mutates the existing list in place.
  • Assuming extend only 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 append to add one object, including an entire list as a nested item.
  • Use extend or += to add elements from one list into another.
  • Use + or unpacking when you want a new combined list instead of mutation.
  • Remember that extend accepts any iterable.
  • List merging is shallow, so nested mutable objects remain shared unless you copy them separately.

Course illustration
Course illustration

All Rights Reserved.