Python write list to file

This article will explain different ways to write list to a file or save the contents of a list to a file. We will also take a look at writing each list element in a new line or writing list elements separated by a comma.

Method 1: Using for loop and write()
Open a file in write mode.
Iterate over the list using a for loop and write each element to the file using its write() function. Example,

# define a list
sites=['google',  'youtube', 'facebook']

# open file in write mode
f=open('websites.txt', 'w')
# loop through list
for site in sites:
    f.write(site+'\n')
# close file
f.close()

Note that here we are appending a new line character(\n) to each list item. This will write each element of the list to a new line.
To write the contents as comma separated values(CSV), append a comma instead of new line.

Instead of appending a new line character, write() function can also be written using format specifiers as

f.write('%s\n'%site)

Method 2: Using print() function
Python print() function is normally used to print list values to console but it has a file option which makes it useful to write list to a file. Example,

# list
sites=['google',  'youtube', 'facebook']
f=open('websites.txt', 'w')
# list to file with print function
print(*sites, sep='\n',file=f)
# close file
f.close()

where,
* is called unpacking operator and it passes the list elements directly to print().
sep is the separator with which the elements will be saved to the file. Here, it is newline which means the elements will be written line by line.
file option is the file object where the list will be saved.

If you omit the sep option, then by default it will write list items separated by a space, since default value of sep is an empty space. So, text file contents with sep omitted will be

google youtube facebook

Method 3: Using join()
Python join() is a function of string class. It takes a list as argument and joins the elements of the list with the string on which it is called, as a separator.
join() returns the result as a string. This string can be written to the file using write() function of file object or by using print() function as shown above. Example,

# define list
sites=['google',  'youtube', 'facebook']
# open file
f=open('websites.txt', 'w')
# convert list to string
sites_str='\n'.join(l)

# write string to file
print(sites_str, file=f)
# close file
f.close()

Method 4: Using writelines()
Convert list to a string with join() as shown above and write the resultant string using writelines() function of file object. Example,

# define list 
sites=['google', 'youtube', 'facebook'] 
# open file 
f=open('websites.txt', 'w') 
# convert list to string 
sites_str='\n'.join(l)
# write string to file 
f.writelines(sites_str, file=f)
# close file
f.close()

If you directly supply the list to writelines(), then it will write list elements one after another without any separator.
So, with f.writelines(sites), file contents will be

googleyoutubefacebook

Method 5: Using pickle module
If you just want to serialize list to a file such that it will be read by a program only and not by a human, then you can use python’s pickle module, which saves list to a file in binary format.

Python pickle module has a function dump(), which takes the list and file object as arguments. To use dump(), file must be opened in wb mode, which stands for binary write mode. Example,

import  pickle

sites=['google',  'youtube', 'facebook']
f=open('websites.txt', 'wb')
pickle.dump(sites,f)

To read the file back, it should be opened in rb mode, which is binary read mode.

That is all on different ways to write list to a file in python. Hope the article was useful.