I have this textfile.txt
:
i
car
air
me
And a dictionary is defined as:
dictionary = {"me":3, "you":4, "else": 10, "i":2}
I'm looking for a way to delete the words in textfile.txt from the dictionary in a generalizable way (I'm using a loop here) My attempt:
words_to_delete = open("textfile.txt", "r")
for i in words_to_delete.readlines():
del dictionary[i]
# Output: KeyError: 'i\n'
Going further, I think is because of this:
for i in words_to_delete.readlines():
print(i == "me")
# Output: False, False, False, False
Why the values from the loop are not comparable from the textfile.txt
?
If I run this:
for i in words_to_delete.readlines():
print(type(i))
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
It's a string, so why the strings i
and me
from the dictionary returns False
when comparing it with brute-force written strings?