I have a text file with the following lyrics:
'First verse Im off till I'm off the island
i'm riding like autoban on autopilot
before I touch dirt I deal you all with kindness
my natual persona much worse'
I want to convert it to a list, then sort it alphabetically, but what I keep getting is: [[First', 'verse', 'Im', 'off', 'till, "I'm", 'off' , 'the', 'island'] [then it prints the third line], [then the second line], [then the fourth line]]
and then it puts it into a list within a list [[ text, text..., ]]
How do I print it so it prints in order in one list and is sorted in alphabetical order using python code? Here is what I have so far:
fname = raw_input("Enter file name: ") #prompts user for .txt file
fhand = open(fname) #reads file
NewList = [] #creating new list to append to
for line in fhand: #for the lines in the text file, read them
line.rstrip() # strip the white space
words = line.split() #split the txt file into string words
for word in words: #for every word in txt file
if words not in NewList: #if not in the NewList
NewList.append(words) #then appended words to Newlist
NewList.sort() #sort it alphabetically
print NewList #then print the sorted list
I'mNewList.extend(words)instead ofNewList.append(words), if I understand you correctly.extendadds all the elements in the argument list to the existing list, whereappendadds the argument list itself to the existing list...