0
0
PythonProgramBeginner · 2 min read

Python Program to Find Union of Lists

You can find the union of two lists in Python by converting them to sets and using the | operator or set.union(), then converting back to a list like list(set(list1) | set(list2)).
📋

Examples

Inputlist1 = [1, 2, 3], list2 = [3, 4, 5]
Output[1, 2, 3, 4, 5]
Inputlist1 = ['a', 'b'], list2 = ['b', 'c', 'd']
Output['a', 'b', 'c', 'd']
Inputlist1 = [], list2 = [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 repeats. Since lists can have duplicates, convert them to sets which automatically remove duplicates, then combine these sets using the union operation, and finally convert back to a list to keep the original data type.
📐

Algorithm

1
Take two input lists.
2
Convert both lists to 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 where list1 = [1, 2, 3] and list2 = [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

{1, 2, 3} | {3, 4, 5} = {1, 2, 3, 4, 5}

3

Convert union set back 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, making it easier to combine unique items.

Step 2: Use union operation

The | operator or set.union() combines two sets to include all unique elements from both.

Step 3: Convert back to list

Since the original data type is a list, converting the set back to a list gives the final union result in list form.

🔄

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 preserves order but is less efficient for large lists.

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

Time Complexity

Converting lists to sets and performing union takes linear time relative to the total number of elements in both lists.

Space Complexity

Extra space is used to store sets and the resulting union list, proportional to the combined size of input lists.

Which Approach is Fastest?

Using set union is fastest and simplest; looping to check membership is slower due to repeated membership checks.

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