The program includes many call of .write() method which is inefficient in itself. Lists on the other hand may eat too much memory for large file.