Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Sherlock Holmes
3. Unique words


Excellent, Sherlock! However, our get_words_list() function returns the list of all the words. Some of them appear twice, or thrice in the list.

Now we would like to find the list of unique words, so that each word appears only once in the list. How can we do that? We can use the set() function to create a set data structure. Sets contain no duplicates and are therefore great for finding unique elements. Here's how you can create a set:

with open('filename.txt') as file: 
  file_data = # we read all contents of the 'filename.txt' 
  file_words_list = file_data.split() # we split the contents into the list 
  unique_words = set(file_words_list) # we cast the list into a set


Create a function called get_words_set() that accepts the name of a text file as its argument and returns a set of unique words from this file.

Use the function on the sherlock.txt file to obtain all the unique words from this file. Save them into a sherlock_unique_words variable.