Find Uncommon Words from Two Strings - Python
Last Updated : 27 Feb, 2025
The task of finding uncommon words from two strings in Python involves identifying words that appear in only one of the given strings while ignoring common words. Given two input strings, the goal is to extract words that are unique to each string. For example, with A = "Geeks for Geeks" and B = "Learning from Geeks for Geeks", the result would be ["Learning", "from"].
Using collections.Counter
Counter from collections module count word occurrences efficiently. It processes both strings, storing word frequencies in a dictionary like structure. The uncommon words are then extracted by filtering words that appear only once. This approach is fast and suitable for handling large datasets.
Python from collections import Counter s1 = "Geeks for Geeks" s2 = "Learning from Geeks for Geeks" # count word occurrences count = Counter(s1.split()) + Counter(s2.split()) # extract uncommon words res = [word for word in count if count[word] == 1] print(res)
Output['Learning', 'from']
Explanation: This program splits both strings into words, counts occurrences using Counter, merges the counts and extracts words that appear only once using list comprehension.
Using set
Set operations identify words that are unique to each string. The symmetric difference (^) extracts words that are present in one string but not the other. It provides a quick and concise way to find uncommon words but does not consider word frequencies within the same string.
Python s1 = "Geeks for Geeks" s2 = "Learning from Geeks for Geeks" # convert to sets set1 = set(s1.split()) set2 = set(s2.split()) # find uncommon words res = list(set1 ^ set2) # symmetric difference print(res)
Output['from', 'Learning']
Explanation: This program splits both strings into sets and uses the symmetric difference (^) to find words that appear in only one set, filtering out common words.
Using get()
This approach manually constructs a dictionary to store word counts from both strings. The get() method ensures efficient updates while iterating over the words. After building the dictionary, uncommon words are extracted by checking their frequency. It is an effective alternative when Counter is not available.
Python s1 = "Geeks for Geeks" s2 = "Learning from Geeks for Geeks" # dictionary for word count d = {} # insert words from s1 and s2 for word in (s1 + " " + s2).split(): d[word] = d.get(word, 0) + 1 # extract uncommon words res = [word for word in d if d[word] == 1] print(res)
Output['Learning', 'from']
Explanation: This program splits and counts words from both strings using a dictionary, then extracts those appearing only once.
Using for loop
This approach manually compares each word with all others using nested loops to count occurrences. It does not require additional data structures but is highly inefficient due to repetitive comparisons. This method is only practical for very small inputs or educational purposes.
Python s1 = "Geeks for Geeks" s2 = "Learning from Geeks for Geeks" words = (s1 + " " + s2).split() res = [] for word in words: if words.count(word) == 1: res.append(word) print(res)
Output['Learning', 'from']
Explanation: This program splits both strings into words, then iterates through them, appending words that appear only once to the result list.
Similar Reads
Python | Union Operation in two Strings One of the string operation can be computing the union of two strings. This can be useful application that can be dealt with. This article deals with computing the same through different ways. Method 1 : Naive Method The task of performing string union can be computed by naive method by creating an
5 min read
Python | Common words among tuple strings Sometimes, while working with tuples, we can have a problem in which we need to find the intersection of words that occur in strings inside single tuple with string as its elements. Let's discuss certain ways in which this problem can be solved. Method #1 : Using join() + set() + & operator + sp
4 min read
Word location in String - Python Word location in String problem in Python involves finding the position of a specific word or substring within a given string. This problem can be approached using various methods in Python, such as using the find(), index() methods or by regular expressions with the re module.Using str.find()str.fi
4 min read
Python - Words Lengths in String We are given a string we need to find length of each word in a given string. For example, we are s = "Hello world this is Python" we need to find length of each word so that output should be a list containing length of each words in sentence, so output in this case will be [5, 5, 4, 2, 6].Using List
2 min read
Python | Extract words from given string In Python, we sometimes come through situations where we require to get all the words present in the string, this can be a tedious task done using the native method. Hence having shorthand to perform this task is always useful. Additionally, this article also includes the cases in which punctuation
4 min read