from collection import Collection text = open('/home/pat/l/en/mobydick.txt','U').read() words = text.split() mobyindex = Collection() for nth, word in enumerate(words): mobyindex[word] = nth out = open('mobydex.txt', 'w') for word, nths in mobyindex.items(): out.write( word + ' ') out.write( repr(nths) ) out.write( '\n') out.close()