How to read a file content into a list in python / Reading a file line by line into a list in python

Reading a text file is a vital part in programming. Mostly when a file is read, its contents are stored in a data structure such as an array, list, map etc. This post will detail out various methods to read a text file line by line and store its contents directly into a list in python.

For a deep understanding about reading and writing files in python, refer this tutorial

Text file that we will be using as an example in this post is named “codippa.txt” and contains following sample content

Hello there
This is python
Learning from codippa.com

Method 1: Using readlines method
Open a file in read mode and read its contents using readlines method directly into a list.

# open file in read mode
with open("codippa.txt", 'r') as file_handle:
    # read file content into list
    lines = file_handle.readlines()
# print list content
print(lines)

This method will contain a new line character after each list item as shown in the output below.

[‘Hello there\n’, ‘This is python\n’, ‘Learning from codippa.com’]

If you want to remove new line character at the end of each item, then iterate over the list and remove new line character using strip method as below.

lines = [line.strip() for line in lines]

Reading file using with and as keywords as with open("codippa.txt", 'r') as file_handle: provides the advantage that file is closed automatically once the task completes. You can also read a file as file_handle = open("f:/linkedFile.txt", 'r') but then you will have to close it manually.

Method 2: Using read and splitlines method
Read the file contents into a list using read method of file object. This method reads the file as it is. For converting file contents into a list, splitlines method is used. This method divides the contents of the file at each line break and inserts each item into a list.
Python docs for splitlines method state

Return a list of the lines in S, breaking at line boundaries.
Line breaks are not included in the resulting list

# open file in read mode
with open("codippa.txt", 'r') as file_handle:
    # convert file contents into a list
    lines = file_handle.read().splitlines()
print(lines)

Output is

[‘Hello there’, ‘This is python’, ‘Learning from codippa.com’]

Note that there is no new line character at the end.

Method 3: Using append function
Open the file for reading. Iterate over the file content line by line using a for loop. In every iteration, add the current line into a list using its append method.

with open("codippa.txt", 'r') as file_handle:
    # initialize list
    lines = []
    # iterate over file contents
    for line in file_handle:
        # add line to list
        lines.append(line.strip())

Note the strip called on the line. This is required to remove new line character from the end of line.
Output is

[‘Hello there’, ‘This is python’, ‘Learning from codippa.com’]

Method 4: Using iteration
There is another approach to convert file contents into a list by using for loop. This method is simpler as compared to above method.

with open("f:/linkedFile.txt", 'r') as file_handle:
    lines = [line.strip() for line in file_handle]

Iterate over file line by line and add it directly to a list. strip function is required to remove new line character from each line. Output is

[‘Hello there’, ‘This is python’, ‘Learning from codippa.com’]

Method 5: Using list function
list function takes an iterable object as argument. It list function returns a list with the contents of the iterable supplied to it as an argument. Python docs for list function state

list(iterable) -> new list initialized from iterable’s items

Example for this approach is given below

with open("codippa.txt", 'r') as file_handle:
    lines = list(file_handle)
# print list content
print(lines)

Output of above code execution is

[‘Hello there’, ‘This is python’, ‘Learning from codippa.com’]

That’s it on converting a file into a list. Keep visiting !!!!

Mark Your Impression

Close Menu