How to Read a Text File Into List in Python?

Python is a powerful programming language which provides different ways of reading a text file into a list. In this tutorial we will explore the methods of reading a text file into a list.

How to Read a Text File Into List in Python?

Reading Text File in Python - How to a Read Text File Into List in Python?

In this tutorial I will show you many examples of Python programs to read a text file into a list. We can also read the text file in one go or line-by-line. You can read files line by line and process the data. Based on your particular scenario you can use any of the methods described here for handling text files in your program.

Benefits of reading a text file into list in Python

First of all we will try to understand the benefits of reading a text file into a list in Python. We will discuss the several benefits of reading it into a list. The use of lists will enhance Python programs in certain cases. By organizing code will enhance the code readability and also provide performance gain. Here are the important benefits of reading a text file into list in Python:

Easy access to any line in file: After reading the file content in a list you will be able to access file content easily and manipulate as per your requirements.

Convenient Iteration and file data processing: You can easily use for or while loop to iterate the content line-by-line efficiently. You can also use transformations and filter functions on each line. All these make file data handling much easier.

Easy access of Data: You can easily access the data from the list and manipulate as per your requirements. You will be able to add, modify and delete data easily.

Better coding: Storing file content in a list makes coding much easier. This will also help you in better managing your code block in your program.

Drawbacks

If you have to process a big text file then storing such content in a list is not advisable and it will slow your program. Often your program may crash due to memory issues. In such scenarios you should read file line by line and process each line at a time.

Video Tutorial: How to Read a Text File Into List in Python?

Reading Text File in Python Examples

Lets start making programs to read a text file in list.

This is the text file "data.txt" that we will be reading in our program:

Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10
Line 11
Line 12
Line 13
Line 14
Line 15
Line 16

Create a text file "data.txt" and add above content in the file.

1. Reading text file in Python

Here is the complete code of reading a text file in the list and then print the list object:

with open('data.txt','r') as file:
    lines = [line.rstrip() for line in file]
    print(lines)
    print(type(lines))

In the above program we are opening a file and reading all lines one and stripping the new line character '\n' from the line. Finally all lines are saved into a list. So, this way you can read a file into a list. Here is the screenshot of the program execution:

Reading text file into list in python

2. Reading Big Text File - Read line by line and process each line data 

If you have a big text file then its advisable to read file line by line and process one time at a time. Saving the content of a big file in a list may crash your program and also performance will be affected. Here is the code to read file line by line and process data one line at a time:

with open('data.txt','r') as file:
    for line in file:
        line_data = line.rstrip()
        # write logic to process data
        print(line_data)

Here is the screenshot of the program execution:

Reading Big Text File in Python

3. Use of walrus operator

Now we will see how to use walrus operator in our program to read a text file. Although we are just reading file one line at a time and printing each line on the console. Here is the example code of reading file using walrus operator:

with open('data.txt','r') as file:
    while line:=file.readline():
            print(line.rstrip())
Here is the screen shot of the program output:

use of walrus operator in Python

4. Reading file in specific encoding

Now we will see how to read a text file in a specific encoding. To read a file in specific encoding you should provide an encoding parameter to the open() function. For example, to read files in UTF-8 encoding you can use encoding="UTF-8" code as a parameter to the open() function. Here is full code of reading a text file in Python in a specific encoding:

with open('data.txt','r', encoding="UTF-8") as file:
    while line:=file.readline():
            print(line.rstrip())

Output of the program is:

Reading text file in specific encoding

5. Reading text file using readlines() method

Now we will see the code for reading a text file using the readlines() method. The readlines() method is a powerful method in Python that reads a text file and runs all lines in list format. Here is the example of readlines() method in Python:

f = open('data.txt','r')
lines = f.readlines()
print(lines)

Output of the readlines() function:

Python readlines() method example

6. Using read() method to read add data in string in one go

Now we will see Python code for using read() method to read and add data in string in one go. The read() method is used to read a specified number of bytes from a file. It takes the number of bytes as a parameter which is used to read bytes from the file. The default value is -1 which means the whole file and function returns the content of the full file. Here is the example code:

f = open('data.txt','r')
file_data = f.read()
print(file_data)

Output of the program:

Python read() method

7. Read line by line and add to list using append() method

Now we will learn to read line by line and add to the list using the append() method. Here we first create a list object and then keep appending each line to the list. Here is the example code of reading file line by line and adding to the list:

data_list = []
with open('data.txt','r') as file:
    for line in file:
        line_data = line.rstrip()
        data_list.append(line_data)

Here is the output of the program:

0

use of append to list method

8. Use of tuple

Now we will show you how you can read text file lines into tuple. Here is the complete example code:

lines = tuple(open('data.txt','r'))
print(lines)

Here is the output of program:

1

use of tuple while reading text file in Python

In this section we learned the different ways of reading text files and processing data. If the file size is small then reading file data into a list is a very convenient method and provides performance benefits.

Related Tutorials

2