I am opening a text file, read it line by line and strip the trailing '\n' and append it to a list with code below. Some words go missing while doing this. Can anybody tell me why?
def compare(t, w):
c = {}
m = []
for line in t:
lines = t.readline()
word = lines.strip()
m.append(word)
for x in m:
c[x] = c.get('x', 0)
if w in c:
print('True')
else:
print('False')
fin = open('words.txt')
compare(fin, 'expect')
Don't use .readline
inside of a for line in t
loop.
Every iteration a new line will be read and in .readline the next line will be read, so you skip one line every iteration.