Get only unique words from a sentence in Python

Question:

Let’s say I have a string that says “mango mango peach”. How can I print only the unique words in that string.
The desired output for the above string would be [peach] as a list
Thanks!!

Asked By: sam24

||

Answers:

First – split you string with empty space delimiter (split() method), than use Counter or calculate frequencies by your own code.

Answered By: Andrii Matiiash
seq = "mango mango peach".split()
[x for x in seq if x not in seq[seq.index(x)+1:]]
Answered By: tomc

You can use a Counter to find the number of occurrences of each word, then make a list of all words that appear only once.

from collections import Counter

phrase = "mango peach mango"

counts = Counter(phrase.split())

print([word for word, count in counts.items() if count == 1])
# ['peach']
Answered By: Patrick Haugh

Python has a built in method called count that would work very well here

text = "mango mango peach apple apple banana"
words = text.split()

for word in words:
    if text.count(word) == 1:
        print(word)
    else:
        pass
(xenial)vash@localhost:~/python/stack_overflow$ python3.7 mango.py 
peach
banana

Using list comprehension you can do this

[print(word) for word in words if text.count(word) == 1]
Answered By: vash_the_stampede

Here is another solution using sets.

sentence = "mango mango peach"

# Convert the sentence into a list
words = sentence.split()
print(words)

# Convert the list into a set. Only unique words will be stored in the set
unique_words = set(words)
print(unique_words)
Answered By: Dennis Njogu
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.