Open In App

Find Uncommon Words from Two Strings – Python

Last Updated : 27 Feb, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

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.

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.

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.

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.

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.



Next Article

Similar Reads

three90RightbarBannerImg