Signup/Sign In

Python program to find uncommon words from two Strings

In this tutorial, you will learn to find uncommon words from a string in Python. Strings in Python are a sequence of characters wrapped in single, double or triple quotes. Words are the substrings that are separated by a space in the original string.

A word is uncommon only if it appears exactly once in any one of the given strings, and does not appear in the other string.

Let us look at the sample input and output of the program.

Input:

"sub string is part of string"

" 1 is part of number system"

Output:

["sub", "1", "number", "system"]

To solve this problem in Python we can follow these approaches:

  1. By hashing
  2. By using iterative method
  3. By using symmetric_difference() method

We will be discussing each approach in detail.

Approach 1: hashing method

Every uncommon word occurs exactly once in any one of the strings. So, we will make a hash to count the number of occurrences of every word, then we will return a list of words that occur only once. To count the occurrence of each word use count.get() method. It is an inbuilt method that returns the number of occurrences of a substring in the given string.

Algorithm

Follow the algorithm to understand the approach better.

Step 1- Define a function that will accept both the strings

Step 2- In the function, declare a variable that will store all the counts

Step 3- For every word in the first string count occurrence of each word

Step 4- Count occurrence of every word of string 2

Step 5- Select and return only those words whose count is equal to 1

Step 7- Print the value returned by the function

Python Program 1

Look at the program to understand the implementation of the above-mentioned approach. We have used list comprehension, which is a shorter syntax for putting values in a list based on certain conditions, to select words that have a count of 1.

def uncommon_words(s1, s2):
    count = {}
    for word in s1.split():
        count[word] = count.get(word, 0) + 1
    # words of string s2
    for word in s2.split():
        count[word] = count.get(word, 0) + 1
    # return required list of words
    return [word for word in count if count[word] == 1]

s1="Studytonight"
s2="Welcome to Studytonight"
  
# Print required answer
print(uncommon_words(s1, s2))


['Welcome', 'to']

Approach 2: iterative method

In this approach, we will first separate out the words in the string using the split() method. Then we will check for words that are in the first string but not in the second string and vice-versa. We will add these words to a string and return this string as the result.

Algorithm

Follow the algorithm to understand the approach better.

Step 1- Define a function that will accept both the strings

Step 2- In the function, declare a list of words of the first string

Step 3- Declare another list for words of the second string

Step 4- Declare a string that will store the uncommon words

Step 5- Search for words that are in string 1 and not in string 2 and add these uncommon words in the new string

Step 6- Search for words that are in string 2 and not in string 1 and add these uncommon words in the new string

Step 7- Return the new string as the result

Step 8- Print the value returned by the function

Python Program 2

Look at the program to understand the implementation of the above-mentioned approach. We have used for loop to traverse through the words in the string.

#list of uncommon words

def uncommon(s1,s2):
    list_s1 = s1.split()
    list_s2 = s2.split()
    uc_words = ""
    for i in list_s1:
        if i not in list_s2:
            uc_words =  uc_words+" "+i
    for j in list_s2:
        if j not in list_s1:
             uc_words =  uc_words+" "+j
  
    return  uc_words
  

a = "berry mango cherry"
b = "berry peach cherry"
print(uncommon(a,b))


mango peach

Approach 3: using symmetric_difference() method

We will be using a method that works for sets in Python. The symmetric_difference() method returns a set that contains all items from both sets, except the items that are present in both sets.

Algorithm

Follow the algorithm to understand the approach better.

Step 1- Define a function that will accept both the strings

Step 2- In the function, declare a variable that will store all the words of string 1

Step 3- Declare another variable that will store all the words of string 2

Step 4- In a variable store the value returned by symmetric_difference()

Step 5- Return this variable

Step 6- Declare both strings and call the function

Step 7- Print the value returned by the function

Python Program 3

Look at the program to understand the implementation of the above-mentioned approach. To convert string to a set we have used the set() method and to convert a data sequence to a list in Python, we have used the list() method.

def uncommon(s1,s2):
  s1=s1.split()
  s2=s2.split()
  k=set(s1).symmetric_difference(set(s2))
  return k

a = "berry mango cherry"
b = "berry peach cherry"
print(list(uncommon(a,b)))


['mango', 'peach']

Conclusion

In this tutorial, we have seen how to find uncommon words from two given strings using three different approaches. First, by using the hashing method, second by using the iterative method, and third by using the symmetric_difference() method.



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.