0
0
PythonProgramBeginner · 2 min read

Python Program to Find Union of Two Lists

You can find the union of two lists in Python by converting them to sets and using the union() method or the | operator like this: union_list = list(set(list1) | set(list2)).
📋

Examples

Input[1, 2, 3], [3, 4, 5]
Output[1, 2, 3, 4, 5]
Input["apple", "banana"], ["banana", "cherry"]
Output["apple", "banana", "cherry"]
Input[], [1, 2]
Output[1, 2]
🧠

How to Think About It

To find the union of two lists, think about combining all unique items from both lists without repeating any. We can do this by turning each list into a set, which automatically removes duplicates, then joining these sets together to get all unique elements combined.
📐

Algorithm

1
Take two input lists.
2
Convert both lists into sets to remove duplicates.
3
Find the union of these two sets to combine unique elements.
4
Convert the union set back to a list.
5
Return or print the resulting list.
💻

Code

python
list1 = [1, 2, 3]
list2 = [3, 4, 5]

union_list = list(set(list1) | set(list2))
print(union_list)
Output
[1, 2, 3, 4, 5]
🔍

Dry Run

Let's trace the example lists [1, 2, 3] and [3, 4, 5] through the code.

1

Convert lists to sets

set(list1) = {1, 2, 3}, set(list2) = {3, 4, 5}

2

Find union of sets

set(list1) | set(list2) = {1, 2, 3, 4, 5}

3

Convert union set to list

list({1, 2, 3, 4, 5}) = [1, 2, 3, 4, 5]

4

Print the result

Output: [1, 2, 3, 4, 5]

OperationValue
set(list1){1, 2, 3}
set(list2){3, 4, 5}
Union{1, 2, 3, 4, 5}
Final list[1, 2, 3, 4, 5]
💡

Why This Works

Step 1: Convert lists to sets

Using set() removes duplicate elements from each list, so we only keep unique items.

Step 2: Find union of sets

The | operator combines both sets, keeping only one copy of each unique element.

Step 3: Convert back to list

We convert the set back to a list because sets are unordered and lists are easier to use for most tasks.

🔄

Alternative Approaches

Using set.union() method
python
list1 = [1, 2, 3]
list2 = [3, 4, 5]

union_list = list(set(list1).union(set(list2)))
print(union_list)
This method is functionally the same but uses the explicit <code>union()</code> method instead of the <code>|</code> operator.
Using a loop to add unique elements
python
list1 = [1, 2, 3]
list2 = [3, 4, 5]

union_list = list1.copy()
for item in list2:
    if item not in union_list:
        union_list.append(item)
print(union_list)
This approach avoids sets and keeps the order of first list, but is slower for large lists.

Complexity: O(n + m) time, O(n + m) space

Time Complexity

Converting lists to sets takes O(n) and O(m) time, where n and m are the lengths of the lists. The union operation also takes O(n + m). Overall, the time is linear in total input size.

Space Complexity

Extra space is needed to store the sets and the union, which is O(n + m) in the worst case when all elements are unique.

Which Approach is Fastest?

Using sets with the | operator or union() method is fastest and simplest. The loop method is slower due to repeated membership checks.

ApproachTimeSpaceBest For
Set union with | operatorO(n + m)O(n + m)Fastest and simplest for unique elements
Set union() methodO(n + m)O(n + m)Same as | operator, more explicit
Loop with membership checkO(n * m)O(n + m)Maintains order but slower for large lists
💡
Use sets to quickly find unique combined elements from two lists without duplicates.
⚠️
Trying to add lists directly without removing duplicates, which results in repeated elements.