Merge Lists in Python

In real-world programming, it’s common to merge lists—whether you’re combining user inputs, appending search results, or aggregating data from multiple sources. Merging helps organize information and streamline operations like filtering, analysis, or reporting. Python, being a versatile language, offers several easy ways to merge lists. Some methods create a new list, while others modify the original. The choice depends on whether you need to preserve the original data or not. In this blog, we’ll explore the most popular and beginner-friendly ways to merge lists in Python, complete with examples and explanations for each method.

Why Merge Lists in Python?

Merging lists plays a key role in handling real-world data. You might need to combine form responses from multiple users, append results from different database queries, or consolidate system logs. Each use case has different performance needs. For instance, extending an existing list is fast and memory-efficient, while creating a new list might be safer if you want to keep the original unchanged. Choosing the right method helps avoid bugs, improves speed, and makes your code easier to manage.

Merge Lists Using Naive Method

# Python Program to Merge Lists Using Naive Method
 
# Initializing lists
list1 = [1, 6, 3, 5, 2, 9]
list2 = [3, 5, 7, 8, 5]
 
# using naive method to merge list
for i in list2 :
    list1.append(i)
 
# Printing merged list
print ("Merged list using naive method : \n"+ str(list1))

Output:

Merged list using naive method : 
[1, 6, 3, 5, 2, 9, 3, 5, 7, 8, 5]

Python Program to Merge Lists Using + operator

# Python Program to Merge Lists Using + operator

# Initializing lists
li3 = [6, 4, 3, 6, 5]
li4 = [5, 8, 6, 4, 9]

# using + operator to merge
li3 = li3 + li4

# Printing Merged list
print ("Merged list using + : \n"+ str(li3))

Output:

Merged list using + : 
[6, 4, 3, 6, 5, 5, 8, 6, 4, 9]

Merge Lists Using Comprehension

# Python Program to Merge Lists Using Comprehension 

# Initializing lists
l1 = [5, 1, 4, 6, 2]
l2 = [6, 9, 7, 2, 5, 6]

# using list comprehension to merge
res_list = [y for x in [l1, l2] for y in x]

# Printing merged list
print ("Merged list using list comprehension: \n" + str(res_list))

Output:

Merged list using list comprehension: 
[5, 1, 4, 6, 2, 6, 9, 7, 2, 5, 6]

Python Program to Merge List Using extend()

# Python Program to Merge Lists Using extend()

# Initializing lists
test_l3 = [1, 4, 5, 6, 5]
test_l4 = [3, 5, 7, 2, 5]

# using list.extend() to merge
test_l3.extend(test_l4)

# Printing merged list
print ("merged list using list.extend() : \n"+ str(test_l3))

Output:

merged list using list.extend() : 
[1, 4, 5, 6, 5, 3, 5, 7, 2, 5]

Comparison Table

MethodIn-place?Preserves OrderEasy to UnderstandBest For
Naive LoopBeginners
+ Operator✅✅Simple merging
List Comprehension✅✅Custom logic
extend()Memory-efficient merging

Real-World Use Case

Let’s say you’re building a form where users submit their favorite movies. You receive multiple lists and want to merge them while maintaining order and clarity.

Here’s a small function using the + operator for quick merging:

def merge_user_lists(list1, list2, method='plus'):
    if method == 'plus':
        return list1 + list2
    elif method == 'extend':
        list1_copy = list1.copy()
        list1_copy.extend(list2)
        return list1_copy
    elif method == 'comprehension':
        return [item for item in list1] + [item for item in list2]
    elif method == 'naive':
        merged = []
        for item in list1:
            merged.append(item)
        for item in list2:
            merged.append(item)
        return merged
    else:
        raise ValueError("Unsupported method")

# Example
list_a = ['apple', 'banana']
list_b = ['cherry', 'date']
print(merge_user_lists(list_a, list_b, method='extend'))

This function gives flexibility based on the method you choose—ideal for real-world applications like data collection or aggregation.

We explored four ways to merge lists in Python: naive loop, + operator, list comprehension, and extend(). Use + for quick merges, extend() for in-place edits, and comprehension for custom filtering. Try merging larger or nested lists.👉 Explore more on Newtum for programming blogs and courses.

About The Author

Leave a Reply