Union Operation of two Strings using Python

The union operation on strings combines two strings while removing duplicate characters. Python provides several approaches to perform string union operations, each with different characteristics and use cases.

Using Sets

Sets automatically remove duplicate elements, making them ideal for union operations ?

def string_union_sets(first, second):
    set1 = set(first)
    set2 = set(second)
    union_result = set1.union(set2)
    return ''.join(union_result)

# Example
first = "What"
second = "Where"
result = string_union_sets(first, second)
print(f"Union of '{first}' and '{second}': {result}")
Union of 'What' and 'Where': Wreath

Using Dictionary Keys

Dictionary keys maintain insertion order (Python 3.7+) while ensuring uniqueness ?

def string_union_dict(first, second):
    union_dict = {}
    for char in first:
        union_dict[char] = True
    for char in second:
        union_dict[char] = True
    return ''.join(union_dict.keys())

# Example
first = "What"
second = "Where"
result = string_union_dict(first, second)
print(f"Union of '{first}' and '{second}': {result}")
Union of 'What' and 'Where': Whater

Using List with Membership Check

Simple approach that preserves order of first occurrence ?

def string_union_list(first, second):
    result_chars = list(first)
    for char in second:
        if char not in result_chars:
            result_chars.append(char)
    return ''.join(result_chars)

# Example
first = "What"
second = "Where"
result = string_union_list(first, second)
print(f"Union of '{first}' and '{second}': {result}")
Union of 'What' and 'Where': Whater

Using Set with Pipe Operator

The pipe operator (|) provides a concise syntax for set union ?

def string_union_pipe(first, second):
    return ''.join(set(first) | set(second))

# Example
first = "What"
second = "Where"
result = string_union_pipe(first, second)
print(f"Union of '{first}' and '{second}': {result}")
Union of 'What' and 'Where': Wreath

Using OrderedDict for Order Preservation

Maintains character order while removing duplicates ?

from collections import OrderedDict

def string_union_ordered(first, second):
    combined = first + second
    return ''.join(OrderedDict.fromkeys(combined))

# Example
first = "What"
second = "Where"
result = string_union_ordered(first, second)
print(f"Union of '{first}' and '{second}': {result}")
Union of 'What' and 'Where': Whater

Comparison

Method Preserves Order? Performance Best For
Sets No Fast Simple union without order concern
Dictionary Yes (insertion order) Fast Order-preserving union
List + membership Yes Slow for large strings Small strings, simple logic
Pipe operator No Fast Concise syntax
OrderedDict Yes Fast Order preservation guaranteed

Conclusion

Use sets or pipe operators for simple union operations when order doesn't matter. For order-preserving union, use dictionary keys or OrderedDict. Choose the method based on your specific requirements for performance and character order preservation.

Updated on: 2026-03-27T10:46:37+05:30

805 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements