Question

How to Read a File Line by Line into a List in Python

pythonstringfilereadlines

Question

How can I read every line of a file in Python and store each line as an element in a list?

I want to process the file line by line and append each line to the end of a list.

Short Answer

By the end of this page, you will understand how to read a text file in Python so that each line becomes one item in a list. You will learn the most common syntax, how newline characters behave, when to use readlines() versus looping over a file, and how this pattern appears in real Python code.

Concept

In Python, a file object is iterable, which means you can loop over it one line at a time using a for loop. This is one of the most common ways to read text files.

When you read a file line by line, each line is returned as a string. If you store those strings in a list, the result is a list where each element represents one line from the file.

This matters because many real programs work with line-based data:

  • log files
  • configuration files
  • CSV-like plain text data
  • lists of usernames, URLs, or IDs
  • imported data for scripts

Python gives you a few ways to do this:

  • iterate through the file and append each line to a list
  • use file.readlines() to get all lines at once
  • use a list comprehension for a compact version

The best choice depends on clarity, file size, and whether you want to clean each line as you read it.

A very important detail: lines often include a trailing newline character like \n. If you do not want that, you usually remove it with strip() or rstrip().

Mental Model

Think of a text file like a stack of paper forms.

  • The file is the whole stack.
  • Each line is one form.
  • Your Python loop picks up one form at a time.
  • The list is a tray where you place each form after reading it.

If the forms have an extra blank margin at the end, that is like the newline character \n. You can keep it, or trim it off before placing the form into the tray.

Take Quiz

Syntax and Examples

The most common beginner-friendly approach is to use with open(...) and loop over the file.

lines = []

with open("data.txt", "r") as file:
    for line in file:
        lines.append(line)

print(lines)

What this does

  • open("data.txt", "r") opens the file for reading
  • with ensures the file is closed automatically
  • for line in file reads one line at a time
  • lines.append(line) adds each line to the list

Removing newline characters

Often you do not want each string to end with \n.

lines = []

with open("data.txt", "r") as file:
    for line in file:
        lines.append(line.strip())

print(lines)

If data.txt contains:

apple
banana
orange

then lines becomes:

['apple', 'banana', 'orange']

Using readlines()

Python also provides a shortcut:

with open("data.txt", "r") as file:
    lines = file.readlines()

print(lines)

This reads all lines into a list at once.

Using a list comprehension

A compact and common style is:

with open("data.txt", "r") as file:
    lines = [line.strip() for line in file]

print(lines)

This is often preferred when you want readable and concise code.

Step by Step Execution

Consider this file content:

cat
dog
bird

And this code:

lines = []

with open("animals.txt", "r") as file:
    for line in file:
        lines.append(line.strip())

print(lines)

Step by step

  1. lines = []

    • An empty list is created.
  2. with open("animals.txt", "r") as file:

    • Python opens the file in read mode.
    • file now refers to the opened file object.
  3. for line in file:

    • Python starts reading the file one line at a time.
  4. First loop iteration:

    • line is 'cat\n'
    • line.strip() becomes 'cat'
    • 'cat' is appended to lines
    • lines is now ['cat']
  5. Second loop iteration:

    • line is 'dog\n'
    • line.strip() becomes 'dog'
    • lines is now ['cat', 'dog']
  6. Third loop iteration:

    • line is 'bird\n'
    • line.strip() becomes 'bird'
    • lines is now ['cat', 'dog', 'bird']
  7. The loop ends when the file has no more lines.

  8. The with block ends.

    • Python closes the file automatically.
  9. print(lines) outputs:

['cat', 'dog', 'bird']

Real World Use Cases

Reading a file line by line into a list is useful in many practical situations.

1. Loading a list of usernames

with open("users.txt") as file:
    users = [line.strip() for line in file]

2. Reading configuration values

with open("hosts.txt") as file:
    hosts = [line.strip() for line in file if line.strip()]

This skips empty lines.

3. Processing logs

with open("app.log") as file:
    log_lines = file.readlines()

Later, your program can search for errors or warnings.

4. Importing plain-text data for scripts

with open("urls.txt") as file:
    urls = [line.strip() for line in file]

This is common in automation and scraping scripts.

5. Preparing data for testing

Developers often store sample input in text files and load each line into a list during tests.

Take Quiz

Real Codebase Usage

In real projects, developers usually combine file reading with cleanup and validation.

Common patterns

Simple line loading

with open("items.txt") as file:
    items = [line.strip() for line in file]

Skip empty lines

with open("items.txt") as file:
    items = [line.strip() for line in file if line.strip()]

Validate values while reading

valid_ids = []

with open("ids.txt") as file:
    for line in file:
        value = line.strip()
        if not value:
            continue
        if value.isdigit():
            valid_ids.append(int(value))

Guard clauses for missing files

from pathlib import Path

path = Path("data.txt")
if not path.exists():
    print("File not found")
else:
    with path.open() as file:
        lines = [line.strip() for line in file]

Error handling

try:
    with open("data.txt") as file:
        lines = [line.strip() for line in file]
except FileNotFoundError:
    lines = []

In real codebases, with open(...) is the standard pattern because it is safe, readable, and handles closing the file automatically.

Common Mistakes

Beginners often run into a few common issues.

1. Forgetting to use with

Broken style:

file = open("data.txt", "r")
lines = file.readlines()

This works, but the file may not be closed properly if something goes wrong.

Better:

with open("data.txt", "r") as file:
    lines = file.readlines()

2. Being surprised by \n

Broken expectation:

with open("data.txt") as file:
    lines = file.readlines()

print(lines)
# Might print: ['apple\n', 'banana\n']

Fix:

with open("data.txt") as file:
    lines = [line.strip() for line in file]

3. Using strip() when only the newline should be removed

strip() removes whitespace from both ends, not just newlines.

text = "  hello  "
print(text.strip())
# 'hello'

If leading spaces matter, use rstrip("\n") instead.

with open("data.txt") as file:
    lines = [line.rstrip("\n") for line in file]

4. Reading a huge file all at once unnecessarily

with open("large.log") as file:
    lines = file.readlines()

This may use a lot of memory for large files. If you only need to process one line at a time, loop over the file instead.

5. Forgetting file encoding when needed

Some files are not plain ASCII or default UTF-8.

with open("data.txt", encoding="utf-8") as file:
    lines = [line.strip() for line in file]

6. Appending outside the loop by mistake

Broken code:

lines = []
with open("data.txt") as file:
    for line in file:
        pass
lines.append(line)

This appends only the last line.

Correct code:

lines = []
with open("data.txt") as file:
    for line in file:
        lines.append(line)

Comparisons

ApproachExampleBest forNotes
Loop + append()for line in file: lines.append(line)Learning and custom processingClear and flexible
readlines()lines = file.readlines()Quick full-file loadingSimple, but reads all lines at once
List comprehensionlines = [line.strip() for line in file]Clean, concise codeCommon in real projects
read()text = file.read()Reading the whole file as one stringNot line-based

strip() vs rstrip("\n")

MethodWhat it removesWhen to use
strip()Whitespace on both endsWhen you want clean values
rstrip()Whitespace on the right sideWhen trailing whitespace should be removed
rstrip("\n")Only newline charactersWhen leading spaces must be preserved

Manual loop vs shortcut methods

  • Use a manual loop if you want to validate, skip, transform, or log each line.
  • Use readlines() if you simply want the lines quickly.
  • Use a list comprehension when you want a concise and readable transformation.
Take Quiz

Cheat Sheet

# Read all lines into a list
with open("data.txt", "r") as file:
    lines = file.readlines()

# Read lines and remove newlines
with open("data.txt", "r") as file:
    lines = [line.strip() for line in file]

# Preserve leading spaces, remove only newline
with open("data.txt", "r") as file:
    lines = [line.rstrip("\n") for line in file]

# Manual append style
lines = []
with open("data.txt", "r") as file:
    for line in file:
        lines.append(line)

Key rules

  • Use with open(...) to close files automatically.
  • A file object can be looped over line by line.
  • readlines() returns a list of strings.
  • Each line may contain \n at the end.
  • Use strip() or rstrip("\n") if needed.
  • For large files, line-by-line iteration is usually better than loading everything unnecessarily.

Common choices

  • Keep raw lines: file.readlines()
  • Clean lines: [line.strip() for line in file]
  • Custom logic: loop with append()

FAQ

Why does each line contain \n in Python?

Because text lines in files usually end with a newline character. Python includes that character when reading the line.

What is the safest way to open a file in Python?

Use with open(...) as file: because it closes the file automatically, even if an error happens.

Should I use readlines() or a for loop?

Use readlines() for quick simple cases. Use a for loop when you want more control or when working with larger files.

How do I remove blank lines while reading a file?

Use a condition in a comprehension:

with open("data.txt") as file:
    lines = [line.strip() for line in file if line.strip()]

Can I store file lines directly into a list without calling append()?

Yes. A list comprehension is a common way:

with open("data.txt") as file:
    lines = [line.strip() for line in file]

How do I keep spaces at the start of a line?

Do not use strip() if leading spaces matter. Use rstrip("\n") instead.

What happens if the file does not exist?

Python raises FileNotFoundError. You can handle it with try and except.

Is reading line by line memory efficient?

Yes, iterating over the file line by line is generally more memory friendly than reading the whole file content at once.

Take Quiz

Mini Project

Description

Build a small Python script that reads a text file of tasks and stores each task as an item in a list. This demonstrates how to read line by line, remove newline characters, and ignore empty lines, which is a common pattern in scripts and small utilities.

Goal

Create a program that loads tasks from a file into a clean Python list and prints the numbered results.

Requirements

  1. Open a text file named tasks.txt for reading.
  2. Read the file line by line.
  3. Store each non-empty line in a list.
  4. Remove newline characters from each line.
  5. Print the final list as a numbered task list.
Take Quiz

Keep learning

Related questions

View more