#! /usr/bin/python file = open('C:\\Users\\Admin\\Desktop\\python\\3letterwords.txt').read() file = file.replace('\r\n','').replace('\r','').replace('\n','') open('3lettersparsed.txt','w').write(file) file = '' words = open('3lettersparsed.txt').read().split(' ') domains = open('5letterdomains.txt').read().split('\n') file2 = open('5letterdomainswords.txt','w') for word in words: for domain in domains: if word.lower() == domain[2:5]: print domain file2.write("%s\n" % domain)