VOOZH about

URL: https://www.geeksforgeeks.org/python/python-read-text-file-into-list-or-array/

⇱ Python - Read Text File into List or Array - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python - Read Text File into List or Array

Last Updated : 23 Jul, 2025

Reading files and storing their contents in an array is a common task in Python. It is mostly used when we need to process file data line by line or manipulate file data as a list of strings.In this article, we will explore different methods to read a file and store its contents in an array (list) efficiently.

Using readlines Method (Most Efficient)

The readlines() method reads all lines in a file and returns them as a list of strings making it the most efficient way to store file contents in an array.

Output (example file content):

['First line\n', 'Second line\n', 'Third line\n']

Let's see some other methods to read files with an Array in Python

Using a Loop to Read Line by Line

This method allows us to read lines one at a time and append them to an array manually. It is useful for large files to avoid memory issues.


Output:

['First line\n', 'Second line\n', 'Third line\n']

Using List Comprehension

List comprehension provides a concise way to read file lines into an array in a single line of code.


Output(example file content):

['First line\n', 'Second line\n', 'Third line\n']

Removing Newline Characters While Reading

To remove the trailing newline characters (\n) from each line while reading the file we can use strip() along with a loop or list comprehension.


Output( example file content)

['First line', 'Second line', 'Third line']

Reading Files as Binary Data

If we need to work with binary data instead of text we can open the file in binary mode ('rb').


Output(example file content)

[b'First line\n', b'Second line\n', b'Third line\n']

Using NumPy to Read File into an Array

If we are working with numerical data the numpy.loadtxt() function can read file contents directly into a NumPy array.


Output(example file content)

['First' 'line' 'Second' 'line' 'Third' 'line']
Comment
Article Tags: