List and append question | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

List and append question

If There are three sentences ,how can I get the every single words of three sentences to be elements in one list ? Ex) txt file is .. Learning python is hard Summer is too hot Be careful not to be exhusted Output i want to see is.. ['learning','python','is','hard','Summer'... ...'exhusted'] I wrote as follow.. file=open(fname) #three sentences in this file ans=list() for line in file: sline=line.split() ans.append(sline) print ans The result is.. 3 elements having words in each sentence.. Help me out..I spent around 2hours to solve this but I almost give up ..

2nd Aug 2016, 2:42 PM
shin eun kyung
shin eun kyung - avatar
2 Answers
+ 1
Solution is as mentioned below: file=open(fname) #three sentences in this file ans=list() for line in file: sline=line.split() for word in sline: ans.append(word) print ans You should add another for loop to parse each word in the split line and then append it the list.
2nd Aug 2016, 7:10 PM
Sruti Sarayan
0
Awe. Thank you very much for your help!!
2nd Aug 2016, 11:20 PM
shin eun kyung
shin eun kyung - avatar