I’ve recently started learning how to code with Python and I’m using the “Python for everybody” book, I have a problem with an exercise in chapter 9 :
“Write a program that reads the words in words.txt and stores them as
keys in a dictionary. It doesn’t matter what the values are. Then you
can use the in operator as a fast way to check whether a string is in the
So,what I came up with is this code :
w = dict() file = open("words.txt") for line in file : words = line.split() for word in words : if word not in w : w[word] = 1 else : w[word] = w[word] + 1 print(w)
I was wondering if there’s a more efficient way to do or I can make it better.