Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Create a list using words in a file

+4 votes
asked Jul 18, 2018 by HosseinKian (160 points)
Write a program to 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 function. For
each word, check to see if the word is already in a list. If the word is
not in the list, add it to the list. When the program completes, sort
and print the resulting words in alphabetical order.

Enter file: romeo.txt
['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']

2 Answers

0 votes
answered Jul 24, 2018 by anonymous

my_line = set();

with open("romeo.txt","r") as f:

     for line in f:

          my_line.update(set(f.readline().split(' ')));

my_list = list(my_line);

print(sorted(my_list));

0 votes
answered Feb 5, 2020 by Nando Abreu (970 points)

Try this working solution:

https://onlinegdb.com/rk4e1HuzI

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...