Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Union Operation of two Strings using Python
Python is a very commonly used language by programmers all over the world for different purposes such as machine learning, data science, web development and to perform many other operations with automation. It has many different features which help us to work on many different projects. One such feature of python is the union operation. The union operation means to combine two different strings into one common string after removing all the common elements in both the string. In this article we are going to learn about different methods which can be used for union operation of two strings.
Different Methods of Union Operation
Sets
Sets is a feature in python provided to store multiple items in one single data set. It has an inbuilt feature of removing all the common elements from the strings. Let's take an example to understand it in a better way:
Example
def multiple_strings(first, second): # The input of both the strings are given
data1 = set(first) # Both the strings are then converted into data sets
data2 = set(second)
union_data = data1.union(data2) # After conversion, the data sets are combined with the help of union operation
final_string = ''.join(union_data) # The Combined data set is then converted back into strings
return final_string
# Example
first = "What" # The two input strings are defined
second = "Where"
final_result = multiple_strings(first, second) # The function multiple_strings is run
print(final_result) # The output after union Operation Will be shown
Output
The output of the above example will be as follows:
Wraeth
Dictionary
In this method we will use the python dictionary for union operation. The dictionary will be used to store all the data as strings and then union operation will be performed over it. The example for union operation through this method is as follows:
Example
def multiple_strings(first, second): # The input of both the strings are given
union_dict = {} # A new dictionary is created for the union operation
for char in first: # All the elements in both the strings are checked and then they are added in the new dictionary created
union_dict[char] = True
for char in second:
union_dict[char] = True # No duplicate characters will be added because dictionary keys will take input of different characters only
union_string = ''.join(union_dict.keys()) # Once the union operation of the keys is performed, then we will convert the dictionary key back into string
return union_string
# Example
first = "What" # The two input strings are defined
second = "Where"
final_result = multiple_strings(first, second) # The function multiple_strings is run
print(final_result) # The output after union Operation Will be shown
Output
The output of the above example will look as follows:
Whater
Checking List & Membership
This is a very simple method of performing the union operation. We will simply convert the string into list for the union operation. The example of this method is as follows:
Example
def multiple_strings(first, second): # The input of both the string is given
combined_strings = list(first) # The first string is converted into a list
for char in second: #If the element in second string is not present in first string then they are combined into the first list and the union operation is performed
if char not in combined_strings:
combined_strings.append(char)
final_string = ''.join(combined_strings) #The lists are then converted back into string
return final_string
# Example
first = "What" # The two input strings are defined
second = "Where"
final_result = multiple_strings(first, second) # The function multiple_strings is run
print(final_result) # The output after union Operation Will be shown
Output
The output of the above example will look as follows:
Whater
Combined Use of Set and Pipe operators
This method is a complex method and should not be used in simple cases of combination. In this method the strings are converted into set and then instead of directly using the union, we will use pipe operators. Let's take an example to understand it in a better way:
Example
def multiple_strings(first, second): # The input of both the string is given
first_set = set(first) # Both the strings are converted into sets
second_set = set(second)
final_string = ''.join(first_set | second_set) # Using the pipe operator the respective sets are combined after removing the common elements
return final_string
# Example
first = "What" # The two input strings are defined
second = "Where"
final_result = multiple_strings(first, second) # The function multiple_strings is run
print(final_result) # The output after union Operation Will be shown
Output
The output of the above example will be as follows:
Wraeth
Itertools Module
The itertools module is used for efficiently checking all the loops present in the data set. It has many different functions which can be used for many different purposes. We will use 2 such different functions to perform the union operation. Let's take an example to understand it in better way:
Example
import itertools # Do not forget to import itertools or else error might occur
def unique_everseen(iterable, key=None):
seen = set()
seen_add = seen.add
if key is None: # The input of both the string is given
for element in itertools.filterfalse(seen.__contains__, iterable):# Through the chain() function we will combine both the strings into cone common string
seen_add(element)
yield element
else:
for element in iterable:
k = key(element)
if k not in seen:
seen_add(k)
yield element
def multiple_strings(first, second): # The input of both the string is given
union_string = ''.join(unique_everseen(itertools.chain(string1, string2)))# With the help of unique.everseen() function we will remove all the common elements from the combined string
return union_string
# Example
first = "What" # The two input strings are defined
second = "Where"
final_result = multiple_strings(first, second) # The function multiple_strings is run
print(final_result) # The output after union Operation Will be shown
Output
The output of the above example will look as follows:
Wraeth
Conclusion
It is very important to have knowledge about different methods that can be used to perform union operation. This article gives knowledge about different methods that can be used for the purpose of performing union operation. One can use any of the above mentioned method as per convenience and field of application.