How to Write a List to a File in Python?

I have spent over a decade building Python applications, from simple automation scripts to complex data pipelines. One task I find myself doing almost every single day is saving data, specifically, taking a Python list and writing it into a file.

It sounds like a basic task, but there are several ways to do it depending on whether you want a simple text file, a formatted CSV, or a structured JSON object.

In this tutorial, I will show you exactly how to write a list to a file using the methods I use in my professional projects.

Method 1: Use Python’s write() Method with a Loop

The easy way to save a list is to use the built-in write() method. I usually reach for this when I want full control over how each item is formatted.

You have to manually add a newline character (\n) if you want each list item on its own line. Otherwise, Python will just smash them all together.

Here is an example using a list of major US tech hubs:

# List of US Tech Hubs
tech_hubs = ["San Francisco", "Austin", "Seattle", "New York", "Boston"]

# Writing to a text file
with open("tech_hubs.txt", "w") as file:
    for city in tech_hubs:
        file.write(city + "\n")
print("Successfully wrote tech hubs to 'tech_hubs.txt'")

You can see the output in the screenshot below.

python write list to file

I always recommend using the with statement. It ensures the file closes automatically, even if something goes wrong during the write process.

Method 2: Use the writelines() Method in Python

If you want to write the entire list at once without looping, writelines() is your best friend. It is more efficient for larger datasets because it reduces the number of I/O operations.

The catch here is that writelines() does not add newlines for you. You must include them in your list items beforehand.

I often use list comprehension to add those newlines on the fly:

# List of top US Universities
universities = ["Stanford", "MIT", "Harvard", "Georgia Tech", "Caltech"]

# Adding newlines and writing all at once
with open("universities.txt", "w") as file:
    file.writelines([univ + "\n" for univ in universities])
print("Successfully wrote universities to 'universities.txt'")

You can see the output in the screenshot below.

python save list to file

This approach is clean, “Pythonic,” and very fast for medium-sized lists.

Method 3: Save a List to a CSV File in Python

When I work with data that needs to be opened in Excel or Google Sheets, I use the csv module. This is the standard for tabular data in the US corporate world.

It handles commas and quoting for you, which is a lifesaver if your list items contain their own punctuation.

import csv

# List of US States and their Capitals
state_data = [
    ["State", "Capital"],
    ["California", "Sacramento"],
    ["Texas", "Austin"],
    ["Florida", "Tallahassee"],
    ["New York", "Albany"]
]

# Writing to a CSV file
with open("us_capitals.csv", "w", newline='') as file:
    writer = csv.writer(file)
    writer.writerows(state_data)
print("Successfully wrote state capitals to 'us_capitals.csv'")

You can see the output in the screenshot below.

write list to file python

Notice the newline=” argument in the open() function. I always include this to prevent extra blank lines from appearing between rows on Windows machines.

Method 4: Use the JSON Module for Structured Data in Python

If I need to save a list that contains more than just strings, like a list of dictionaries or nested lists, JSON is the way to go.

JSON is the universal language of the web. It preserves the structure of your data so you can easily load it back into Python later.

import json

# List of US National Parks with details
national_parks = [
    {"name": "Yellowstone", "state": "Wyoming", "established": 1872},
    {"name": "Yosemite", "state": "California", "established": 1890},
    {"name": "Grand Canyon", "state": "Arizona", "established": 1919}
]

# Saving as a structured JSON file
with open("national_parks.json", "w") as file:
    json.dump(national_parks, file, indent=4)
print("Successfully saved national parks data to 'national_parks.json'")

You can see the output in the screenshot below.

python write list to file with newline

I like using the indent=4 parameter because it makes the resulting file human-readable rather than one long, messy line of text.

Method 5: Write with Python’s print() Function

This is a “pro-tip” I often share with beginners. You can actually use the print() function to write to a file by using the file parameter.

It automatically handles the string conversion and adds a newline for you, making your code very concise.

# List of popular US Sports
sports = ["Baseball", "Basketball", "American Football", "Ice Hockey"]

with open("us_sports.txt", "w") as file:
    for sport in sports:
        print(sport, file=file)

I find this method particularly useful for quick debugging or generating simple logs. I hope this tutorial helped you understand the different ways to save your data.

Each of these methods has its own strengths. For simple text, write() or writelines() is perfect. For data analysis, stick with csv. If you are building a web app or saving settings, json is the gold standard.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.