You are currently viewing Python Read a File line-by-line into a List?

Python How to read a file line-by-line into a list? There are several different methods you can use to read a file line-by-line into a list in Python. In this article, we will use the readlines() method, using a for loop, using list comprehension, using the fileinput module, and using the mmap module.

Advertisements

Related: How to read a text file into a string and strip newlines?

1. Quick Examples of Reading a File Line-by-line into a List

These examples provide a high-level overview of several different methods for reading a file line-by-line into a list. We will discuss them in detail in upcoming sections.


# Quick examples of reading file line by line into list

# Method 1: Using readlines() method
with open('filename.txt', 'r') as f:
    lines = f.readlines()

# Method 2: Using for loop
with open('filename.txt', 'r') as f:
    lines = []
    for line in f:
        lines.append(line.strip())

# Method 3: Using list comprehension
with open('filename.txt', 'r') as f:
    lines = [line.strip() for line in f]

# Method 4: Using map() function
with open('filename.txt', 'r') as f:
    lines = list(map(str.strip, f))

2. readlines() to Read a File into a List in Python

The readlines() method is one of the most common ways to read a file line-by-line into a list in Python. This method returns a list of all the lines in the file, where each line is an element in the list.

Related: You can .List all Files in a Directory in Python.


# Read file into a list
with open('filename.txt', 'r') as f:
    lines = f.readlines()

The readlines() method is straightforward to use and memory-efficient. It only loads the lines that are needed into memory.

The readlines() includes the newline character (\n) at the end of each line, which may require extra processing to remove if you don’t need it.

3. Iterating Over a File with a for Loop

Another common method for reading a file line-by-line into a list in Python is to use a for loop. We initialize an empty list, open the file in read mode using the with statement and iterate over each line in the file using a for loop. Each line is appended to the list using append(), with the newline character (\n) stripped using the strip() method.


# Using for loop
lines = []
with open('filename.txt', 'r') as f:
    for line in f:
        lines.append(line.strip())

The strip() method is used to remove the newline character at end of each line, using this can be slow for very large files.

4. List Comprehension – One Liner Solution

List comprehension is another way to read a file line-by-line into a list. It allows you to create a list in a single line of code.


# Using list comprehension
with open('filename.txt', 'r') as f:
    lines = [line.strip() for line in f]

5. fileinput Module – Files into a List Line by Line

The fileinput module is a built-in Python module that provides a way to read one or more files. It’s designed to be used as a command-line interface, but it can also be used in Python scripts.

Example of how to use the fileinput module to read a file line-by-line into a list:


# Import fileinput module
import fileinput

lines = []
for line in fileinput.input('filename.txt'):
    lines.append(line.strip())

6. mmap Module – Reading Large Files to a List

The mmap module provides a way to memory-map a file. Memory mapping is a technique that allows you to access the contents of a file as if it were in memory, rather than reading the entire file into memory at once.


# Import mmap module
import mmap

with open('filename.txt', 'r') as f:
    # Memory-map the file
    mmapped_file = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)

    # Iterate over each line in the file
    lines = []
    while True:
        line = mmapped_file.readline()
        if not line:
            break
        lines.append(line.strip())

    # Close the memory-mapped file
    mmapped_file.close()

7. Summary and Conclusion

We have discussed several ways to read a file line-by-line into a list in Python. The readlines() method is simple and concise, but may not be suitable for very large files. for loop and list comprehension methods are also simple and memory-efficient, but may not be as flexible as other methods. If you have any questions, please leave them in the comment section.

Happy Coding!