This is a homework assignment that I have worked on for hours. Have made progress but am at the end of my rope! I have a text file that I have converted to a list of words (including some capitalized words) which I have sorted in alphabetical order. The last thing to do is remove duplicate words from the list. I have found answers to questions about removing items from lists, but not about removing duplicate items. I have set up a loop which -- for reasons I cannot understand - only works on half of the original list.
Here is the code I have tried:
fhand=open('romeo.txt')
data=fhand.read()
data=data.split()
data[0]='but'
data[8]='it'
data[13]='juliet'
data[17]='arise'
data[25]='who'
data.sort()
newlist=[]
for x in data:
if data[0] == data[1]:
del data[0]
elif data[0] != data[1]:
newlist.append(data[0])
del data [0]
print(newlist)
Original split text file is: ['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']
Expected output is: ['already', 'and', 'arise', 'breaks', 'but', 'east', 'envious', 'fair', 'grief', 'is', 'it', 'juliet', 'kill', 'light', 'moon', 'pale', 'sick', 'soft', 'sun', 'sun', 'the', 'the', 'the', 'through', 'what', 'who', 'window', 'with', 'yonder']
Actual output is: ['already', 'and', 'arise', 'breaks', 'but', 'east', 'envious', 'fair', 'grief', 'is', 'it', 'juliet', 'kill', 'light']
So the loop does what it is supposed to do but quits after 'light'. Can't figure this out.