im only getting one line from the file on the list.
fname = input("Enter file name: ")
fh = open(fname)
lst = list()
newlist=list()
for line in fh:
word=line.split()
for words in word:
if words not in lst:
word.append(lst)
lst.sort()
print(lst)
Hi. Welcome.
I’m thinking you might want to change word.append(lst)
to lst.append(words)
. Not sure yet. The indentation will have an effect on the result. Can you edit your post with fences (putting code between lines with ```) and add indentation? Example
```
fh = open(fname)
lst = []
...
if words not in lst:
word.append(lst)
...
```
fname = input("Enter file name: ")
fh = open(fname)
lst = list()
…
for line in fh:
…word=line.split()
…
for words in word:
… if words not in last:
… lst.append(words)
… lst.sort()
print(lst)
The way you have it there, the first loop runs through all lines in the file and updates word
. When that loop is done, only the last line is captured in word
. To “see” the updates to word
, you want to indent the second loop inside the block of the first loop.
fname = input("Enter file name: ")
fh = open(fname)
lst = []
for line in fh:
words = line.split()
for word in words:
if word not in lst:
lst.append(word)
lst.sort()
print(lst)
fh.close()