Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Find Uncommon Words from Two Strings - Python
Next article icon

Find Uncommon Words from Two Strings - Python

Last Updated : 27 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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.


Next Article
Find Uncommon Words from Two Strings - Python

S

Sanjit_Prasad
Improve
Article Tags :
  • Strings
  • Pattern Searching
  • Python Programs
  • DSA
Practice Tags :
  • Pattern Searching
  • Strings

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences