Adding brackets to a list and sort it

Hello,

I have the following assignment:

Open the file romeo.txt and read it line by line. For each line, split the line into a list of words using the split() method. The program should build a list of words. For each word on each line check to see if the word is already in the list and if not append it to the list. When the program completes, sort and print the resulting words in python sort() order as shown in the desired output.
You can download the sample data at http://www.py4e.com/code3/romeo.txt

The output must be the following:

[‘Arise’, ‘But’, ‘It’, ‘Juliet’, ‘Who’, ‘already’, ‘and’, ‘breaks’, ‘east’, ‘envious’, ‘fair’, ‘grief’, ‘is’, ‘kill’, ‘light’, ‘moon’, ‘pale’, ‘sick’, ‘soft’, ‘sun’, ‘the’, ‘through’, ‘what’, ‘window’, ‘with’, ‘yonder’]

The following is the code I wrote:

fname = input("Enter file name: ")
fh = open(fname)
lst = list(fh)
for line in lst:
liner = line.split()
new_lst = str(liner)[1:-1]
print(new_lst)

The output I’m getting is an unsorted list of words without brackets.
I tried to use the sort() function but I get an error. I believe that sort() function only works when there are brackets, am I right?
Could someone please help on putting the brackets on the beginning and the end of the set of words and sort them?

Thanks in advance.

Your code is not indented properly for one.

Explain what you are trying to accomplish with above line.

I added this line to remove the brackets of all lines. If I didn’t do it, I would get the following output:
[‘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’]

The process I took:

  1. read the file using with open
  2. read all lines
  3. split the lines into words list
  4. loop through words in the line list
  5. add to empty list if the word not already there.

I would post my solution but, I’m relatively new here and not sure how the admins are about posting a possible answer.

That line is just creating a string of words and commas. That is not what you want to do.

I recommend following the algorithm @menator01 mentioned above. Then all you would need to do is sort the list.

Thank you for your help.

# File to read
file = 'romeo.txt'

# Open file and read all lines. / Creates a list of lines
with open(file, 'r') as myfile:
    lines = myfile.readlines()

# Create an empty list
words = []

# Loop through lines list and split the lines into words.
# Creates a list of words in each line
for line in lines:
    for word in line.split():

        # If a word is not in our empty word list append
        if word not in words:
            words.append(word)

# Sort and print word list
words.sort()
print(words)

Thanks a lot for your help!