Create list from txt file, sort, and append. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Create list from txt file, sort, and append.

I know I know I can do this with a for loop but I want to do it this way first. I need to create a list from a text file, sort it, then remove any repeated words. I have created a list and sorted it. need to append the list and remove any repeated words and can't figure out how. this is what I have.. ( again yes I know a for loop would likely be cleaner but I want to do it this way first) fname = input("Enter file name: ") fh = open(fname) words = fh.read() words2 = words.rstrip() words3 = words2.split() words3.sort() print(words3) works fine up to this point.. but I know I will need a loop after this point to remove and repeated words. ['Arise', 'But', 'It', 'Juliet', 'Who', 'already', 'and', 'and', 'and', 'breaks', 'east', 'envious', 'fair', 'grief', 'is', 'is', 'is', 'kill', 'light', 'moon', 'pale', 'sick', 'soft', 'sun', 'sun', 'the', 'the', 'the', 'through', 'what', 'window', 'with', 'yonder']

2nd Nov 2017, 8:17 PM
Aric Dunn
Aric Dunn - avatar
3 Answers
+ 8
You can convert it to set() before sort() Sets can only contain unique elements so you'll have your job done this way :)
2nd Nov 2017, 8:21 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 7
Oh, it's really simple. You should read it almost like in English: "if i (is) not in words4, append (it)." Another words: if it is already in words4, do not append it again. But I prefer a non-loop solution with set ;) words3 = sort(set(words2.split())) print(words3)
2nd Nov 2017, 8:46 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
0
Well I just randomly too a stab it with this.. fname = input("Enter file name: ") fh = open(fname) words = fh.read() words2 = words.rstrip() words3 = words2.split() words3.sort() words4 = [] for i in words3: if i not in words4: words4.append(i) print(words4) and it worked fine.. however I do not understand the for loop very well. especially the "not in" .. it does not make much logical sense to me.
2nd Nov 2017, 8:36 PM
Aric Dunn
Aric Dunn - avatar