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
- The Problem to Solve
- Implementation Example: Merging Project Members
- Source Code
- Execution Result
- 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_bcreates 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 oflist_bsequentially to the end oflist_a. - Feature:
list_aitself is modified (in-place processing). It does not return a new list (returnsNone). - 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).
