In this tutorial, you’ll learn How to write a list to a file in python. Data structures such as lists, dictionaries, arrays, etc. are used to store data. This data must be written in a file or some database so that it can be used again. This article explains different methods along with examples by which you can store lists by writing them into a file. If you want to learn more about Python Programming, Visit Python Programming Tutorials.
4 common ways to write a list to a file in Python
Some of the common ways to write a list to a file in Python are:
- WRITE A LIST TO A FILE USING A FILE.WRITE() COMMAND
- USE PICKLE MODULE TO WRITE A LIST TO A FILE
- USE JSON MODULE FOR WRITING A LIST TO A FILE
- WRITING A LIST TO A FILE USING WRITELINES() COMMAND
WRITE A LIST TO a FILE USING A FILE.WRITE() COMMAND
First of all, create a list variable that you want to store in the file. Then open the file using the open() command in which you want to store the data. In the example below, the ‘file.txt’ is the file in which we want to store the data. Access the elements using for loop and write them one by one in the ‘file.txt’ using the textfile.write() command. After this, simply close the file. Now, to check whether the data has been stored in the file or not, open the file. You’ll observe that the file will contain the data of the list.
#Using file.write() Command list = ["faizan", "zeeshan", "nouman"] textfile = open("file.txt", "w") for element in list: textfile.write(element + "\n") textfile.close()
file.txt faizan zeeshan nouman
uSE PICKLE MODULE TO WRITE A LIST TO FILE
The second method uses the pickle module which converts any data object such as lists, dictionaries, etc. into a byte stream of 0s and 1s before storing it. This process is known as serialization. In real-world scenarios, the data is usually transmitted in the form of byte streams. The data when reaches the destination, it is then unpickled or deserialized to obtain the original information or data transmitted. To write in binary format, open the ‘listfile’ and write ‘wb’ in the open statement. Then, using the pickle.dump() command, write into the ‘listfile’. If you want to see the output of the file in the console, write the pickle.load() command to see its content as shown in the output below.
# Using Pickle Module import pickle list = ["faizan", "zeeshan", "nouman"] with open('listfile', 'wb') as fp: pickle.dump(list, fp) with open('listfile', 'rb') as fp: names = pickle.load(fp) print('List is', names)
List is ['faizan', 'zeeshan', 'nouman']
USE JSON MODULE FOR WRITING A LIST TO FILE
The above two methods are used when you need to store a list in a .txt file. In this method, we’ll cover how to store data from a list into a JSON file. JSON is the writing format for storing and exchanging data. Python provides a built-in package named JSON to deal with the data or files in JSON format. First of all, import the JSON library and open the json file. In the example below, ‘names.json’ is a JSON file. Open the file using the open command and for writing in the binary format, we write ‘wb’ in the open statement. Then, use the json.dump() command to write into the file.
# Using Json Module import json list = ["faizan", "zeeshan", "nouman"] with open("names.json", "w") as fp: json.dump(list, fp) with open('names.json', 'rb') as fp: names = json.load(fp) print('List is', names )
List is ['faizan', 'zeeshan', 'nouman']
WRITING A LIST TO A FILE USING WRITELINES() COMMAND
Another method is to use writelines() command. Open a file filelist.txt as shown in the example below and write the command writelines(). Write the keyword ‘w’ in the open statement to write into the file. The writelines() command will write the data into a file without inserting any space between two items of the list.
# Using Writelines() Commad list = ["faizan", "zeeshan", "nouman"] with open('filelist.txt', 'w') as fp: fp.writelines(list)
The output obtained in the filelist.txt after executing the code is shown below.
To write a list to a file, four different methods are used.
If you have any queries regarding this article, please let us know.