Most efficient way to Write huge data to a file with python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Most efficient way to Write huge data to a file with python

Lets say i have a huge list that have 10000000 item on it and i want to write every item on each line on a file i know i can do it like this F= open("myfile.txt","w") For i in my list : F.write("".join(i)+"/n") F.close But that mean i call f.write()10000000 times and that take some time i want to a methode to write all data at once Thank you I have found answer in stackoverflow but i didnt understand

27th Mar 2019, 2:36 AM
Zamoslovski
2 Answers
+ 4
Why to call write() method millions times? That's what making it slower. Rather store your data in a Python data structure ( choose wisely according to your data) and use write() method once to write the entire data to the file. It'll make it faster and efficient.
27th Mar 2019, 5:15 AM
Шащи Ранжан
Шащи Ранжан - avatar
0
Yeah that what i want to do but how to store my data in python data structure ?? Give an example pls, my data are on a list like this mylist = ["my","list"] or a tuple
27th Mar 2019, 9:26 PM
Zamoslovski