Common words among tuple strings in Python

When it is required to find common words among tuple strings, the join() method, the set() method, the & operator and the split() method can be used together.

The join() method can be used to join multiple values based on a specific delimiter. Python comes with a datatype known as set. This set contains elements that are unique only. The set is useful in performing operations such as intersection, difference, union and symmetric difference.

The split() function splits the given data into multiple sections depending on the delimiter. The & operator performs intersection (AND operation) between sets.

Example

Below is a demonstration of finding common words among tuple strings ?

my_tuple_1 = ('Hi there', 'Hi Will,', 'Hi ', 'Hi there')

print("The tuple is : ")
print(my_tuple_1)

# Find intersection of words from first three strings
my_result = ", ".join(sorted(set(my_tuple_1[0].split()) & 
                            set(my_tuple_1[1].split()) &
                            set(my_tuple_1[2].split())))

print("Common words among the tuples are : ")
print(my_result)
The tuple is : 
('Hi there', 'Hi Will,', 'Hi ', 'Hi there')
Common words among the tuples are : 
Hi

Using a More General Approach

For finding common words across all strings in a tuple, we can use a more scalable approach ?

my_tuple = ('Python is great', 'Python programming is fun', 'is Python easy')

print("The tuple is:")
print(my_tuple)

# Convert all strings to sets of words
word_sets = [set(string.split()) for string in my_tuple]

# Find intersection of all sets
common_words = set.intersection(*word_sets)

# Join and sort the result
result = ", ".join(sorted(common_words))

print("Common words among all strings:")
print(result)
The tuple is:
('Python is great', 'Python programming is fun', 'is Python easy')
Common words among all strings:
Python, is

How It Works

The process involves the following steps:

  • Split each string ? Each string in the tuple is split into individual words using split()
  • Convert to sets ? Each list of words is converted to a set to remove duplicates
  • Find intersection ? The & operator finds common elements between sets
  • Sort and join ? Common words are sorted alphabetically and joined with commas

Conclusion

Use set intersection with the & operator to find common words among tuple strings. The set.intersection() method provides a more scalable solution for multiple strings.

Updated on: 2026-03-25T17:28:37+05:30

376 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements