[Python] How to Merge Lists: Comparing the + Operator and extend

In Python, there are two main ways to combine multiple lists into one: using the + operator and using the extend method of a list object.

While the result of “combining” is the same, their behaviors differ significantly regarding whether they modify the original list (destructive vs. non-destructive).

In this article, I will explain the characteristics of each and how to use them appropriately.

目次

Table of Contents

  1. The Problem to Solve
  2. Implementation Example: Merging Project Members
  3. Source Code
  4. Execution Result
  5. Explanation

The Problem to Solve

Consider a scenario where you want to create a company-wide list by merging employee lists from different departments, or when you want to append additional data to an existing dataset.

You should choose the method based on whether you need to preserve the original data or prioritize memory efficiency by overwriting the existing list.


Implementation Example: Merging Project Members

Here, we will demonstrate the difference between the two methods using a scenario of merging member lists from a “Dev Team” and an “Ops Team”.

Source Code

# 1. Merging using the + operator (Non-destructive)
# Creates a new list while keeping the original lists intact

dev_team = ["Alice", "Bob"]
ops_team = ["Charlie", "Dave"]

# Combine two lists to create a new list 'project_members'
project_members = dev_team + ops_team

print("--- Result of + Operator ---")
print(f"Dev Team (Unchanged): {dev_team}")
print(f"Ops Team (Unchanged): {ops_team}")
print(f"Project Members     : {project_members}")


# 2. Merging using the extend method (Destructive)
# Modifies (extends) the original list itself

core_members = ["Eve", "Frank"]
new_joiners = ["Grace", "Heidi"]

print("\n--- Before extend method ---")
print(f"Core Members: {core_members}")

# Add contents of new_joiners to core_members itself
core_members.extend(new_joiners)

print("--- After extend method ---")
print(f"Core Members (Changed): {core_members}")
# new_joiners itself is unaffected
print(f"New Joiners           : {new_joiners}")

Execution Result

--- Result of + Operator ---
Dev Team (Unchanged): ['Alice', 'Bob']
Ops Team (Unchanged): ['Charlie', 'Dave']
Project Members     : ['Alice', 'Bob', 'Charlie', 'Dave']

--- Before extend method ---
Core Members: ['Eve', 'Frank']
--- After extend method ---
Core Members (Changed): ['Eve', 'Frank', 'Grace', 'Heidi']
New Joiners           : ['Grace', 'Heidi']

Explanation

1. Merging with the + Operator

  • Behavior: list_a + list_b creates and returns a new list containing elements from both lists.
  • Feature: The original lists (list_a, list_b) remain unchanged.
  • Use Case: Use this when you want to keep the original data intact and handle the combined data separately.

2. Merging with the extend() Method

  • Behavior: list_a.extend(list_b) adds the elements of list_b sequentially to the end of list_a.
  • Feature: list_a itself is modified (in-place processing). It does not return a new list (returns None).
  • Use Case: Use this when you want to update the original list directly or when you want to save memory costs by avoiding the creation of a new list (especially useful when handling large lists).
よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次