Find the Union of Two Lists in Python

In Python, the union of two lists refers to combining all unique elements from both lists, ensuring no duplicates. This can be achieved using set operations, loops, or list comprehensions.


Examples

1. Union of Two Lists using the set() and union() Method

The easiest way to find the union of two lists is by converting them into sets with set() builtin function and then using the set union() method.

</>
Copy
# Define two lists
list1 = [1, 2, 3, 4]
list2 = [3, 4, 5, 6]

# Find the union using set
union_list = list(set(list1).union(set(list2)))

# Print the result
print("Union of Lists:", union_list)

Explanation:

We first convert both lists (list1 and list2) into sets. The set().union() method is then used to combine both sets while automatically removing duplicates. Finally, we convert the result back into a list using the list() function.

Output:

Union of Lists: [1, 2, 3, 4, 5, 6]

2. Union of Two Lists using the | (Union) Operator with Sets

We can also use the | operator, which is a shorthand for the union() method.

</>
Copy
# Define two lists
list1 = ["apple", "banana", "cherry"]
list2 = ["banana", "orange", "grape"]

# Find the union using | operator
union_list = list(set(list1) | set(list2))

# Print the result
print("Union of Lists:", union_list)

Explanation:

We use the | operator between the two sets set(list1) and set(list2), which combines their elements while removing duplicates. The final result is converted back to a list.

Output:

Union of Lists: ['apple', 'banana', 'cherry', 'orange', 'grape']

3. Union of Two Lists using List Comprehension

If we don’t want to use sets, we can manually filter unique elements using list comprehension.

</>
Copy
# Define two lists
list1 = [10, 20, 30, 40]
list2 = [30, 40, 50, 60]

# Find union using list comprehension
union_list = list1 + [item for item in list2 if item not in list1]

# Print the result
print("Union of Lists:", union_list)

Explanation:

We first add all elements of list1 to union_list. Then, we iterate through list2 and append only those elements that are not already in list1. This ensures uniqueness without using sets.

Output:

Union of Lists: [10, 20, 30, 40, 50, 60]

4. Union of Two Lists using itertools.chain() and set()

The itertools.chain() function allows us to efficiently merge multiple lists, and we can use set() to remove duplicates.

</>
Copy
import itertools

# Define two lists
list1 = [100, 200, 300]
list2 = [200, 400, 500]

# Find union using itertools.chain and set
union_list = list(set(itertools.chain(list1, list2)))

# Print the result
print("Union of Lists:", union_list)

Explanation:

We use itertools.chain() to merge list1 and list2. The resulting iterable is then converted into a set to ensure uniqueness before converting back to a list.

Output:

Union of Lists: [100, 200, 300, 400, 500]

Conclusion

Python provides multiple ways to find the union of two lists:

  1. set().union(): The easiest and most readable approach.
  2. | Operator: A more compact alternative to union().
  3. List Comprehension: Avoids sets but manually filters unique elements.
  4. itertools.chain(): Useful for merging large lists efficiently.

Using sets is the most efficient method when dealing with large datasets, while list comprehension provides better control over element filtering.