Convert List to TXT Text File & Vice Versa in Python (Examples)

 

Hi! This tutorial will show you how to transform a list to a txt file and vice-versa in the Python programming language.

First, though, here is an overview of this tutorial:

Let’s dive into Python code!

 

Create Sample List

We are going to create the sample Python list that we will convert to a txt file in this tutorial. Therefore, in your Python coding IDE, run the line of code below to create the sample list:

fruits = ["Apple", "Banana", "Oranges", "Peach", "Pear", "Watermelon"]
 
 
print(type(fruits))
#<class 'list'>

As seen, it is a list containing 6 strings and, indeed, has the list type in the Python language.

 

Example 1: List to TXT | Transform List to TXT File Using write() Method

In this first example, we will use Python’s write() method to turn the list to a txt file:

with open("output.txt", "w") as file:
    for item in fruits:
        file.write(item + "\n")

In the code above, we created a new txt file called “output.txt” and iteratively wrote each item of the list to a new row "\n" of the file using a for loop.

You should see the “output.txt” file if you check your working directory, which can be found by calling the getcwd() function of the os module.

 

Example 2: List to TXT | Transform List to TXT File Using writelines() Method

In this next example, we will use the built-in writelines() method to transform the list to a txt file “output.txt2”:

with open("output.txt2", "w") as file:
    file.writelines("%s\n" % item for item in fruits)

In the code, we used the "%s\n" symbol to format the string with a new line character at the end of each item before it is written to the file.

Once again, you can check your working directory to open the “output.txt2” file.

 

Example 3: List to TXT | Transform List to TXT File Using write() and join() Methods

In this third example, we will combine the write() method with the join() method to turn the list to a txt file:

with open("output.txt3", "w") as file:
    file.write("\n".join(fruits))

In the above code, we used the join() method inside the write() method to write each item in the list on a separate line in the txt file named “output3.txt”.

Now that we have seen ways to convert a list to a txt file, we will examine how to convert the txt file back into a list.

Example 1: TXT to List | Transform TXT File to List Using readlines() Method & List Comprehension

In this example, we will use Python’s readlines() method to read the txt file “output.txt”, then we will use list comprehension to turn it into a list:

with open("output.txt", "r") as file:
    fruits_list = file.readlines()
 
fruits_list = [line.strip() for line in fruits_list]
print(fruits_list)
 
# ['Apple', 'Banana', 'Oranges', 'Peach', 'Pear', 'Watermelon']
 
 
print(type(fruits_list))
 
# <class 'list'>

In the above code, we first read the file via file.readlines(), then we used list comprehension [line.strip() for line in fruits_list] using the strip() method to iteratively unpack the content of the file into a list.

 

Example 2: TXT to List | Transform TXT File to List Using For Loop

In this second example, we will use a for loop to turn output.txt file into a list:

fruits_list = []
 
with open("output.txt", "r") as file:
    for line in file:
        fruits_list.append(line.strip())
 
print(fruits_list)
 
# ['Apple', 'Banana', 'Oranges', 'Peach', 'Pear', 'Watermelon']
 
 
print(type(fruits_list))
 
# <class 'list'>

We first read in the “output.txt” file, then looped through the items in the file and appended those items to a list object. The strip() function is used to remove any leading or trailing whitespace from each value.

With that, we have demonstrated how to convert a list to a txt file and vice-versa in the Python programming language. I hope you found this tutorial helpful!
 

Video, Further Resources & Summary

Do you need more explanations on how to convert a list to a txt file and vice-versa in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to convert a list to a txt file and vice-versa in Python.

 

The YouTube video will be added soon.

 

Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:

This post has shown how to convert a list to a txt file and vice-versa in Python. In case you have further questions, you may leave a comment below.

 

R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top