-5

fname = input("Enter file name: ")

if len(fname) < 1 : fname = "romeo.txt"

fh = open(fname)

lst = []

loop over the text

for lin in fh: # split the lines lin = lin.rstrip() lin = lin.split() # loop over the split lines for a in lin: # write a condit to determine if a word is unique # append the word to the empty list lst.append(a)

lst.sort() print(lst)

https://www.py4e.com/code3/romeo.txt?PHPSESSID=4eb3426c73615745fa14760acf0d7a88
  • Welcome to [Stack Overflow.](https://stackoverflow.com/ "Stack Overflow") Please be aware this site can help solve specific, technical problems, not open-ended requests. Please edit your question to show what you have tried so far. See the [How To Ask a Good Question](https://stackoverflow.com/help/how-to-ask "How To Ask a Good Question") and ["How do I ask and answer homework questions?"](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions "How do I ask and answer homework questions?") pages for details on how to best help us help you. – itprorh66 Jul 30 '21 at 23:10

1 Answers1

0
text = """
But soft what light through yonder window breaks
It is the east and Juliet is the sun
Arise fair sun and kill the envious moon
Who is already sick and pale with grief
"""
words = list(dict.fromkeys(list(filter(None, text.replace("\n", " ").split(" ")))))

The Code Splits the words, removes empty strings, then removes duplicates:

W3schools remove duplicates

Remove empty strings from a list of strings

awdr
  • 64
  • 1
  • 8