Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
The sentence is to be or not to be. How many words does it have, and how many different words?
len(words) counts all. len(set(words)) counts the different ones.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, compare your working with the solution.
split() gives the list of words: to, be, or, not, to, be. That is 6 words.
set(words) keeps each word once: to, be, or, not. That is 4 different words.
Log in to join the discussion.
Loading…