How do I return all the unique words from a text file using Python? For example:
I am not a robot
I am a human
Should return:
for word in word_list:
if word not in word_list:
every word
is in word_list
, by definition from the first line.
Instead of that logic, use a set:
unique_words = set(word_list)
for word in unique_words:
file.write(str(word) + "\n")
set
s only hold unique members, which is exactly what you're trying to achieve.
Note that order won't be preserved, but you didn't specify if that's a requirement.