Python - Combine two lists by maintaining duplicates in first list

In data analysis using Python, we often need to combine two lists while handling duplicate elements carefully. This article shows how to combine two lists by maintaining all elements from the first list (including duplicates) and adding only unique elements from the second list.

Using extend() with Generator Expression

This approach preserves the original first list and uses a generator expression to filter unique elements from the second list ?

# Given lists
first_list = ['A', 'B', 'B', 'X']
second_list = ['B', 'X', 'Z', 'P']

# Create result list starting with first list
result = list(first_list)

# Extend with unique elements from second list
result.extend(item for item in second_list if item not in result)

print(result)

The output of the above code is ?

['A', 'B', 'B', 'X', 'Z', 'P']

Using Set Difference

We can use set operations to find elements that exist in the second list but not in the first list, then combine them ?

# Given lists
first_list = ['A', 'B', 'B', 'X']
second_list = ['B', 'X', 'Z', 'P']

# Convert to sets to find unique elements
first_set = set(first_list)
second_set = set(second_list)

# Find elements unique to second list
unique_elements = second_set - first_set
result = first_list + list(unique_elements)

print(result)

The output of the above code is ?

['A', 'B', 'B', 'X', 'P', 'Z']

Using List Comprehension

A more concise approach using list comprehension to filter and combine the lists ?

# Given lists
first_list = ['A', 'B', 'B', 'X']
second_list = ['B', 'X', 'Z', 'P']

# Combine using list comprehension
result = first_list + [item for item in second_list if item not in first_list]

print(result)

The output of the above code is ?

['A', 'B', 'B', 'X', 'Z', 'P']

Comparison of Methods

Method Preserves Order Memory Efficiency Best For
extend() Yes Good In-place modification
Set difference Partial Excellent Large lists
List comprehension Yes Good Readable one-liners

Conclusion

Use extend() for in-place modification, set operations for performance with large lists, or list comprehension for readable one-line solutions. All methods preserve duplicates from the first list while adding only unique elements from the second.

Updated on: 2026-03-15T18:34:41+05:30

485 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements