How to Read Text File Into List in Python?
Last Updated :
27 May, 2025
In Python, reading a text file into a list is a common task for data processing. Depending on how the file is structured—whether it has one item per line, comma-separated values or raw content—different approaches are available. Below are several methods to read a text file into a Python list using the file data.txt as an example.

Using readline()
readlines() method reads all lines from the file and returns them as a list of strings. Each line includes the newline character \n at the end.
Python
with open('data.txt', 'r') as file:
a = file.readlines()
print(type(a), a)
Output
<class 'list'> ['Hello geeks,\n', 'Welcome to geeksforgeeks \n']
Explanation: readlines() method reads the whole file and returns a list where each element is a line, including newline characters. This list is stored in a.
Using read().splitlines()
This method reads the entire file content as a string, then splits it into a list of lines without the newline characters.
Python
with open('data.txt', 'r') as file:
a = file.read().splitlines()
print(type(a), a)
Output
<class 'list'> ['Hello geeks,', 'Welcome to geeksforgeeks ']
Explanation: read().splitlines() reads the entire file as a string and splits it into a list of lines without newline characters.
Using list comprehension
If your file contains numbers, you can read each line, strip whitespace, and convert it to an integer using a list comprehension.
Python
with open('Numeric_data.txt', 'r') as file:
a = [int(line.strip()) for line in file]
print(type(a), a)
Output
<class 'list'> [1, 2, 3, 4, 5]
Explanation: This code reads each line from the file, removes any extra spaces, converts the line to an integer and stores all these integers in a list a.
Using list constructor
list() function can take the file object directly and convert its lines into a list. This method behaves similarly to readlines().
Python
with open('data.txt', 'r') as file:
a = list(file)
print(type(a), a)
Output
<class 'list'> ['Hello geeks,', 'Welcome to geeksforgeeks ']
Explanation: This code converts the file object directly into a list, where each element is a line from the file (including newline characters).
Similar Reads
How to read large text files in Python? In this article, we will try to understand how to read a large text file using the fastest way, with less memory usage using Python. To read large text files in Python, we can use the file object as an iterator to iterate over the file and perform the required task. Since the iterator just iterates
3 min read
How to Split a File into a List in Python In this article, we are going to see how to Split a File into a List in Python. When we want each line of the file to be listed at consecutive positions where each line becomes an element in the file, the splitlines() or rstrip() method is used to split a file into a list. Let's see a few examples
5 min read
How to read numbers in CSV files in Python? Prerequisites: Reading and Writing data in CSV, Creating CSV files CSV is a Comma-Separated Values file, which allows plain-text data to be saved in a tabular format. These files are stored in our system with a .csv extension. CSV files differ from other spreadsheet file types (like Microsoft Excel
4 min read
How to Search and Replace Text in a file in Python In this article, weâll explore four effective methods to search and replace text within a file using Python. These methods range from basic built-in functions to powerful modules like re and fileinput.Method 1: Using Basic File Handling (open() and replace())Let see how we can search and replace tex
3 min read
How to read specific lines from a File in Python? Text files are composed of plain text content. Text files are also known as flat files or plain files. Python provides easy support to read and access the content within the file. Text files are first opened and then the content is accessed from it in the order of lines. By default, the line numbers
3 min read
How to read multiple text files from folder in Python? Reading multiple text files from a folder in Python means accessing all the .txt files stored within a specific directory and processing their contents one by one. For example, if a folder contains three text files, each with a single line of text, you might want to read each fileâs content and proc
3 min read