How to Write a List to CSV in Python

When I was working on a project where I needed to export lists of sales records into a CSV file for easy sharing with colleagues.

The challenge was simple: I had my data in Python lists, but I wanted it in a format that Excel and Google Sheets could open instantly.

After trying a few approaches, I realized Python gives us multiple ways to write lists to CSV files. Some are quick and built-in, while others give you more flexibility with large datasets.

In this tutorial, I’ll walk you through four easy methods I personally use to write a list to CSV in Python. I’ll explain each method step by step, share full code examples, and give you my insights from using them in real-world projects.

Method 1 – Use Python’s Built-in csv Module

The simplest way to write a list to a CSV file is by using Python’s built-in csv module. I often use this method when I need a quick export and don’t want to install extra libraries.

import csv

# Sample list of employee records
employees = [
    ["Name", "Department", "Salary"],
    ["John Smith", "Finance", 80000],
    ["Emma Johnson", "Marketing", 75000],
    ["Michael Brown", "IT", 90000],
    ["Sophia Davis", "HR", 70000]
]

# Writing list to CSV file
with open("employees.csv", "w", newline="") as file:
    writer = csv.writer(file)
    writer.writerows(employees)

print("CSV file created successfully!")

You can see the output in the screenshot below.

save list to csv python

It’s fast, reliable, and works out of the box, but it doesn’t handle very large datasets as efficiently as other methods.

Method 2 – Write List of Dictionaries with csv.DictWriter

Sometimes, instead of Python lists of lists, my data comes as a list of dictionaries. In such cases, DictWriter from the csv module is more convenient.

Here’s how it works:

import csv

# List of dictionaries
students = [
    {"Name": "Alice", "Grade": "A", "City": "New York"},
    {"Name": "Bob", "Grade": "B", "City": "Chicago"},
    {"Name": "Charlie", "Grade": "A", "City": "Los Angeles"},
    {"Name": "Diana", "Grade": "C", "City": "Houston"}
]

# Writing list of dictionaries to CSV
with open("students.csv", "w", newline="") as file:
    fieldnames = ["Name", "Grade", "City"]
    writer = csv.DictWriter(file, fieldnames=fieldnames)

    writer.writeheader()
    writer.writerows(students)

print("CSV file with student data created successfully!")

You can see the output in the screenshot below.

python list to csv

This method is especially useful when your data already comes from APIs or databases in dictionary format.

Method 3 – Use Pandas DataFrame

For larger datasets, I prefer using Python Pandas. It’s fast, flexible, and widely used in data analysis.

Here’s an example where I save monthly sales data into a CSV file:

import pandas as pd

# List of lists
sales_data = [
    ["Month", "Region", "Sales"],
    ["January", "East", 120000],
    ["January", "West", 95000],
    ["February", "East", 135000],
    ["February", "West", 110000],
    ["March", "East", 150000],
    ["March", "West", 125000]
]

# Convert list to DataFrame
df = pd.DataFrame(sales_data[1:], columns=sales_data[0])

# Save DataFrame to CSV
df.to_csv("sales.csv", index=False)

print("Sales data saved successfully using Pandas!")

You can see the output in the screenshot below.

python save list to csv

This creates a professional CSV file without row numbers. If you’re working with large datasets or plan to analyze data later, Pandas is the most powerful option.

Method 4 – Use NumPy savetxt()

If you’re working with numerical data, NumPy provides a very efficient way to export lists (or arrays) to CSV in Python.

Here’s an example with temperature readings from different US cities:

import numpy as np

# Numerical data (temperature in Fahrenheit)
temperatures = [
    [75, 80, 85],
    [60, 65, 70],
    [55, 58, 62],
    [90, 92, 95]
]

# Convert list to NumPy array
temp_array = np.array(temperatures)

# Save to CSV
np.savetxt("temperatures.csv", temp_array, delimiter=",", fmt="%d")

print("Temperature data saved successfully with NumPy!")

This method is super fast and works well when you don’t need headers or text fields. It’s lightweight and efficient, but not ideal if your data includes text or requires headers.

So these are four practical ways to write lists to CSV in Python.

  • Use the built-in csv.writer for quick and simple exports.
  • Use csv.DictWriter when working with dictionaries.
  • Use Pandas when you need advanced data handling.
  • Use NumPy for large numerical datasets.

I’ve personally used all of these methods in real-world projects, from exporting employee records to saving temperature readings.

Choose the method that fits your data and workflow best, and you’ll find writing lists to CSV in Python is easier than you think.

You may also like to read other Python articles:

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.