Python List Union
## Python List Union: How to Merge Lists and Remove Duplicates
In Python, a **union** of two lists is a collection containing all unique elements from both lists, with any duplicate values removed.
Since Python lists can contain duplicate elements and maintain a specific order, the most efficient and idiomatic way to find their union is by leveraging Python's built-in `set` data structure. A `set` is an unordered collection of unique elements.
This tutorial covers how to perform a list union in Python using sets, along with alternative methods depending on your specific requirements (such as preserving the original element order).
---
## Method 1: Using the `set.union()` Method (Recommended)
The standard way to find the union of two lists is to convert both lists into sets, perform the union operation, and then convert the resulting set back into a list.
### Code Example
```python
# Define two lists with overlapping elements
list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]
# Convert lists to sets to remove duplicates
set1 = set(list1)
set2 = set(list2)
# Find the union of the two sets
union_set = set1.union(set2)
# Convert the resulting set back to a list
union_list = list(union_set)
print(union_list)
```
### Output
```python
[1, 2, 3, 4, 5, 6, 7, 8]
```
### Code Explanation
1. **`list1` and `list2`** are initialized with some overlapping elements (`4` and `5`).
2. **`set(list1)` and `set(list2)`** convert the lists into sets. This step automatically filters out any duplicate values within the individual lists.
3. **`set1.union(set2)`** merges the two sets, ensuring that the final collection contains only unique elements from both sets.
4. **`list(union_set)`** converts the unique collection back into a standard Python list.
---
## Method 2: Using the Set Union Operator (`|`)
Python provides a cleaner, shorthand syntax for the union operation using the bitwise OR operator (`|`). This behaves identically to the `.union()` method.
### Code Example
```python
list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]
# Convert to sets, perform union using '|', and convert back to list in one line
union_list = list(set(list1) | set(list2))
print(union_list)
```
### Output
```python
[1, 2, 3, 4, 5, 6, 7, 8]
```
---
## Method 3: Preserving Element Order (Order-Preserving Union)
Because Python sets are unordered, converting a list to a set and back does **not** guarantee that the original order of elements will be preserved.
If maintaining the original order of elements is important for your application, you can use `dict.fromkeys()` (available in Python 3.7+), which preserves insertion order while removing duplicates.
### Code Example
```python
list1 = [5, 1, 2, 3, 4]
list2 = [4, 5, 6, 8, 7]
# Concatenate lists and use dict.fromkeys() to remove duplicates while preserving order
union_list = list(dict.fromkeys(list1 + list2))
print(union_list)
```
### Output
```python
[5, 1, 2, 3, 4, 6, 8, 7]
```
---
## Summary of Methods
| Method | Syntax | Preserves Order? | Performance |
| :--- | :--- | :--- | :--- |
| **Set Union Method** | `list(set(a).union(b))` | No | Extremely Fast ($O(N + M)$) |
| **Set Union Operator** | `list(set(a) \| set(b))` | No | Extremely Fast ($O(N + M)$) |
| **Dictionary Keys** | `list(dict.fromkeys(a + b))` | **Yes** | Fast ($O(N + M)$) |
YouTip