1. HowTo
  2. Python How-To's
  3. Write a List to a File With Python

Write a List to a File With Python

Created: November-16, 2020 | Updated: December-10, 2020

  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 example 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]                          

Contribute

DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Python List

  • Convert Set to List in Python
  • Get Length of the List in Python

    Related Article - Python File

  • Convert List to Tuple in Python
  • Sort List by Another List in Python
  • Ezoic