How to Write a List to a File With Python

  1. Use a Loop to Write a List to a File in Python
  2. Use the Pickle Module to Write a List to a File in Python
  3. Use join() Method to Write a List to a File

This tutorial explains how to write a list to a file with Python. Since there are multiple ways of writing a list to a file, the tutorial also lists various example codes to explain them further.

Use a Loop to Write a List to a File in Python

Using a loop to write a list to a file is a very trivial and most used approach. A loop is used to iterate over the list items, and the write() method is used to write list items to the file.

We use the open() method to open the destination file. The mode of opening the file shall be w that stands for write.

An exmaple code is given below:

listitems = ["ab", "cd", "2", "6"]

with open('abc.txt', 'w') as temp_file:
    for item in listitems:
        temp_file.write("%s\n" % item)
file = open('abc.txt', 'r')
print(file.read())

Output:

{'6','2', 'ab', 'cd'}

For Python 2.x, you also use the following approach:

listitems = ["ab", "cd", "2", "6"]

with open('xyz.txt', 'w') as temp_file:
    for item in listitems:
        print >> temp_file, item

Both of the codes will result in writing a list to a file with Python.

Use the Pickle Module to Write a List to a File in Python

The Pickle module of Python is used to serialize or de-serialize a Python object structure. If you want to serialize a list for later use in the same Python file, pickle could be used. As the pickle module implements binary protocols, so the file shall also be opened in the binary writing mode - wb.

pickle.dump() method is used to write a list to a file. It takes the list and reference of the file as its parameters.

An example code for this approach is given as:

import pickle

listitems = ["ab", "cd", "2", "6"]
with open('outputfile', 'wb') as temp:
    pickle.dump(listitems, temp)
with open ('outfile', 'rb') as temp:
    items = pickle.load(temp)

print(items)

Output:

{'ab','cd', '6', '2'}

Use join() Method to Write a List to a File

Another more straightforward approach to writing a list to a file in Python is by using the join() method. It takes the items of the list as input.

An example code to use this method is shown below:

items = ["a", "b", "c", "d"]

with open("outputfile", "w") as opfile:
    opfile.write("\n".join(items))

Use JSON Module to Write a List to a File

The use of the pickle module is very specific in Python. JSON makes different programs more efficient and easy to understand. JSON module is used to write mixed variable types to a file.

It uses the dump() method that takes a list of elements and references to the file as its input.

An example code is given as:

import json

itemlist = [21, "Tokyo", 3.4]
with open('opfile.txt', 'w') as temp_op:
with open('opfile.txt', 'r') as temp_op:
  templist = json.load(temp_op)
print(templist)

Output:

[21, "Tokyo", 3.4]

Related Article - Python List

  • How to Find Duplicates in a List in Python
  • How to Flatten a List in Python
  • Related Article - Python File

  • How to Create a List With a Specific Size in Python
  • How to Get Unique Values From a List in Python