Read a File line by line into a List in Python

In this Python tutorial, you will learn to read a file line by line into a list.

Table Of Contents

In all the examples, we will read a text file “thisptr_file1.txt”. This file resides in parallel with the python file i.e. in same directory.Content of this file is,

This is the first line.
Second line is also a small line.
Second Last line is the third line.
Last line is the fourth line of file.

Now let’s see different ways to read this file line by line and store all lines in a list.

Read a file line by line into a list using readlines()

First open the file and get a file object. Then use the readlines() method of file object to read the contents of file into a list. This method reads the file line by line and pushes the data in list through a file pointer specified in the “with command”. Finally, we are excluding new line character – “\n” using the strip() method.

Syntax:

with open("file.txt") as var:
    inp_list = var.readlines()

where,
the file is the file name and var is the file pointer that refers to the file.

Example:

In this example, we will consider the file named – thisptr_file1.txt and read by line.

# Open the file and read all lines into a list
with open("thisptr_file1.txt") as var:
    #read the lines
    listOfLines = var.readlines()

print("Actual List: ")

for line in listOfLines:
    print(line)

print()

# Strip newline characters and whitespaces 
# from both the end of the lines
listOfLines = [data.strip() for data in listOfLines]

# Exclude new line characters
print("After Removing new lines: ")

for line in listOfLines:
    print(line)

Output:

Actual List: 
This is the first line.

Second line is also a small line.

Second Last line is the third line.

Last line is the fourth line of file.

After Removing new lines: 
This is the first line.
Second line is also a small line.
Second Last line is the third line.
Last line is the fourth line of file.

We can see that In the first output, new line characters are present. But in the second output, we excluded the newline characters using the strip() method.

Read a file line by line into a list using list comprehension

Here, we will use the list comprehension to read the file into a list. This method takes line by line into a list comprehension through a file pointer specified in the “with command”. Finally, we are excluding new line character – \n using strip()

Syntax:

with open("file.txt") as var:
    inp_list = [data for data in var]

Where,
1. file is the file name and var is the file pointer that refers to the file.
2. data is a variable to iterate line.

Example:
In this example, we will consider the file named – thisptr_file1.txt and read by line into list comprehension.

# Open the file and read all lines into a list
with open("thisptr_file1.txt") as var:
    # read the lines
    listOfLines = [data for data in var]

print("Actual List: ")

for line in listOfLines:
    print(line)

print()

# Strip newline characters and whitespaces 
# from both the end of the lines
listOfLines = [data.strip() for data in listOfLines]

# Exclude new line characters
print("After Removing new lines: ")

for line in listOfLines:
    print(line)

Output:

Actual List: 
This is the first line.

Second line is also a small line.

Second Last line is the third line.

Last line is the fourth line of file.

After Removing new lines: 
This is the first line.
Second line is also a small line.
Second Last line is the third line.
Last line is the fourth line of file.

We can see that In the first output, new line characters are present. But in the second output, we excluded the characters using the strip() method.

Read a file line by line into a list using for loop

Here, we will use for loop to iterate line by line in a file using a file pointer. After that use append() method to append the lines to our list by excluding new line characters using the strip() method. It takes \n that represent the new line characters.

Syntax:

with open("file.txt") as var:
    for data in var:
        inp_list.append(data.strip('\n'))

Where,
1. file is the file name and var is the file pointer that refers to the file.
2. data is a variable to iterate line.

Example:
In this example, we will consider the file named – thisptr_file1.txt and read by line through for loop.

# Open the file and read all lines into a list
with open("thisptr_file1.txt") as var:
    listOfLines = []
    # read the lines
    for data in var:
        listOfLines.append(data.strip('\n'))

print("Actual List: ")

for line in listOfLines:
    print(line)

Output:

Actual List: 
This is the first line.
Second line is also a small line.
Second Last line is the third line.
Last line is the fourth line of file.

You can see that all lines from a file are appended to the list by removing newline characters.

Read a file line by line into a list using splitlines()

Here, we will import Path from the pathlib module. Path is used to load the file. After that, we are using the read_text() method with splitlines() to read file by splitting new lines.

Syntax:

data = Path('file.txt')
inp_list = data.read_text().splitlines()

Where,
1. file is the file name.
2. data is the Path object.

Example:

In this example, we will consider the file named – thisptr_file1.txt and read by line using splitlines().

from pathlib import Path

# Read the file
data = Path('thisptr_file1.txt')

# Split the lines
listOfLines = data.read_text().splitlines()

for line in listOfLines:
    print(line)

Output:

This is the first line.
Second line is also a small line.
Second Last line is the third line.
Last line is the fourth line of file.

Read a file line by line into a list using list comprehension with the strip()

In this case, we need to use list comprehension by stripping new lines(\n) using strip().

Syntax:

[data.strip() for data in open('thisptr_file1.txt')]

Here, data is used to iterate file.

Example:

# Read the file contents into a list
listOfLines = [data.strip() for data in open('thisptr_file1.txt')]

for line in listOfLines:
    print(line)

Output:

This is the first line.
Second line is also a small line.
Second Last line is the third line.
Last line is the fourth line of file.

You can see that new line characters are removed.

Read a file line by line into a list using splitlines() with read()

Here, we are iterating line by line in a file using read() and then split lines using the splitlines() method.

Syntax:

with open("thisptr_file.txt") as var:
    data = var.read() 
inp_list = data.splitlines()

Where data is the iterator to iterate file contents line by line and var is the file pointer.
Let’s see the example.

Example:

# Consider the file
with open("thisptr_file1.txt") as var:
  # Read line by line
    data = var.read() 

# Split the new line charcaters
inp_list = data.splitlines()
print(inp_list)

Output:

[ 'This is the first line.',
  'Second line is also a small line.',
  'Second Last line is the third line.',
  'Last line is the fourth line of file.']

Summary

In this tutorial, you learned about six different approaches to read a file line by line into a list.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top