In this Python Programming article, I will explain how to read a file into a list in Python using various methods with examples.
To read a file into a list in Python we can use readlines() function as it returns a list of all lines, or list comprehension with the strip() function for no newline character, or a for loop with the strip() function, or read() with splitlines() function to create a list of all lines in the file.
Methods to read a file into a list in Python
There are four different methods to read a file into a list in Python:
- Using readlines()
- List Comprehension with strip()
- for loop with strip()
Let’s see them one by one using some demonstrative examples:
Method 1: Read a file into a list in Python using readlines() functions
The readlines() method is probably the most straightforward way to read a text file and store its content in a Python list. When we call file.readlines() on a file object, Python reads all the lines from the file and stores them in a list.
Each element in the Python list corresponds to a line in the file, including the newline character \n at the end of each line.
Example: Consider a situation, where we have a text file with us, and we have to see the data in the form of a list in Python, where each item in the list represents each line from the file through Python.
with open('test.txt', 'r') as file: lines = file.readlines() print(lines)
Output: The implementation of the code with the screenshot and the file data are below:
['California\n', 'Texas\n', 'Florida\n', 'New York\n', 'Alaska']
This way we can use the readlines() function to read a file into a list in Python.
Method 2: Python read file into a list using List Comprehension with strip() function
To avoid having the newline character at the end of each line, a common technique is to use list comprehension along with the strip() method.
List comprehension provides a concise way to apply an operation to each item in a sequence (or iterable) in Python.
In this case, for each line in the file, strip() is called to remove whitespace from the beginning and end of the line, which includes the newline character. This leaves us with a clean list of lines in Python.
Example: For instance, we have to read a file in the form of a list in Python using list comprehension with the strip() function.
with open('test.txt', 'r') as file: lines = [line.strip() for line in file] print(lines)
Output: The output of the Python code is given Below, with a screenshot of the implementation and file data.
['George Washington', 'Abraham Lincoln', 'Franklin D. Roosevelt', 'John F. Kennedy', 'Barack Obama']
The list comprehension with strip() function can be used to read a file into a list in Python.
Method 3: How to read a file line-by-line into a list using a for loop with strip() function
When dealing with very large files, the most memory-efficient way is to iterate over the file object itself.
When we use a for loop on a file object, Python reads each line one at a time, allowing us to process and append it to a list without ever having the whole file in memory. This method is very efficient for large files since it doesn’t require reading the entire file content at once.
Example: Here, we have a file and we have to read the file in a list in Python using a for loop.
lines =  with open('test.txt', 'r') as file: for line in file: lines.append(line.strip()) print(lines)
Output: The implementation of the code is given below the file data screenshot.
['Apple', 'Google', 'Amazon', 'Microsoft', 'Facebook']
This way we can use a for loop with the strip() function can help to read a file into a list in Python.
Method 4: How to Read Text File Into List in Python using read().splitlines() function
An alternative to readlines() is the combination of read() and splitlines(). The read() method reads the whole file into a single string. Then, calling splitlines() on that string splits it into a list where each element is a line from the file in Python.
Unlike readlines(), splitlines() does not include the newline character at the end of each line.
Example: For instance, we have a text file and we have to read it into a list through Python using the read() and splitlines() functions.
with open('test.txt', 'r') as file: lines = file.read().splitlines() print(lines)
Output: The implementation of the Python code with the screenshot is given below, and also the file data.
['Statue of Liberty', 'Grand Canyon', 'Mount Rushmore', 'Golden Gate Bridge', 'Empire State Building']
This way we can use the read() function with the splitlines() function to read a file into a list in Python.
It explains how to read a file into a list in Python can be achieved in several ways. Whether we use readlines(), iterate over the file object using a for loop with a split() function, list comprehension with a split() function, or use a combination of read() and splitlines() functions, each method serves the purpose with slight differences in their memory efficiency and handling of newlines.
Always remember to handle files with the appropriate encoding and to use them with a statement to ensure that files are properly closed after their operations are completed.
You may also like to read:
- Read the file as a string in Python
- Remove Newlines from a String in Python
- Python Program to Write List to File
- How to rename a file in Python
- How to Unpack a List in Python
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.