HOWTO · Python

How to Write List to CSV Columns in Python

This tutorial demonstrates how to write a list to CSV columns in Python using the built-in CSV module, pandas, and NumPy. Learn how to match your data layout to a CSV writing method.

On this page

To write parallel Python lists as CSV columns, combine their corresponding values with zip() and pass the resulting rows to csv.writer. Python’s built-in csv module is enough for this common task. Use nested lists directly when your data is already arranged as rows; use pandas or NumPy when those libraries and their data structures fit your project.

Write parallel lists as CSV columns with csv.writer

Each input list represents one column. zip() takes the first item from each list, then the second item from each, and so on. Writing each resulting tuple as a CSV row places values from the same position side by side. Open CSV files with newline="" so the CSV module can manage line endings consistently.

Run this example:

import csv
from pathlib import Path
from tempfile import TemporaryDirectory

names = ["Alice", "Bob", "Charlie"]
ages = [30, 25, 35]
cities = ["New York", "Los Angeles", "Chicago"]

with TemporaryDirectory() as directory:
    path = Path(directory) / "people.csv"
    with path.open("w", newline="", encoding="utf-8") as file:
        writer = csv.writer(file)
        writer.writerow(["Name", "Age", "City"])
        writer.writerows(zip(names, ages, cities))

    print(path.read_text(encoding="utf-8"), end="")
Name,Age,City
Alice,30,New York
Bob,25,Los Angeles
Charlie,35,Chicago

The file contains a header followed by one record per person: each row has a name, age, and city. The temporary directory makes the example self-contained; replace its path with your desired destination to keep the file after the script ends.

zip() stops as soon as the shortest input list is exhausted. If list lengths differ, values remaining in longer lists are not written. When every value must be preserved, compare the lengths before writing and decide how to handle the mismatch, such as rejecting the data or filling missing values. If there are many lists, give each one a clear name and include each in the same zip() call.

Write rows to a CSV file with csv.writer

If your data is already row-oriented, use a nested list in which each inner list is one record. Call writerows() once to write all records, or writerow() to write one row at a time. For example, rows = [["Alice", 30], ["Bob", 25]] writes two records with two fields apiece. This is different from parallel lists: the nested-list form describes rows, while zip() converts parallel columns into rows for the CSV file.

A header is an ordinary first row, so add one explicitly when readers need column names. The CSV writer quotes fields containing commas, quote characters, or line breaks as needed. It does not infer headers from Python variable names.

Write lists as CSV columns with pandas

If a project already uses pandas, construct a DataFrame from a dictionary whose keys are column names and whose values are lists, then call to_csv(..., index=False). The index=False argument prevents pandas’ row labels from becoming an extra CSV column. For instance, the relevant operations are pd.DataFrame({"Name": names, "Age": ages}) and df.to_csv("people.csv", index=False).

This is a usage illustration, not an executed example in this article. pandas is an additional dependency; install and manage it in your project before using this approach. The lists supplied for DataFrame columns need compatible lengths.

Write numeric arrays as CSV with NumPy

For numeric array data, NumPy provides savetxt; set delimiter="," to write comma-separated values. A typical call is np.savetxt("measurements.csv", array, delimiter=","). This option suits numeric arrays, including two-dimensional arrays where each array row becomes a file row. It is not the general choice for mixed text and numeric columns; use the built-in CSV module for that case.

This NumPy call is a usage illustration and was not executed here. NumPy is an additional dependency, and formatting options may be needed when you require particular decimal precision or a specific representation of numeric values.

Choose the method for your data

Use the built-in csv module when you need a lightweight, standard-library solution. Combine parallel lists with zip() to put corresponding values in columns, or pass nested lists when you already have rows. Choose pandas when your data is already in DataFrames and you want its indexing and data-manipulation features. Choose NumPy’s savetxt for numeric arrays. In each case, decide whether the CSV needs a header and check that column data aligns before writing.

FAQ

What is a CSV file?

CSV means comma-separated values. It is a text format for tabular data. CSV writers handle quoting fields that contain commas, quotes, or line breaks, so prefer a CSV library to joining values with commas yourself.

Do I need to install a library to write CSV in Python?

No. Python includes the csv module. pandas and NumPy are optional third-party dependencies for projects that need their data structures or features.

Can I write a nested list to a CSV file?

Yes. Use writerows() with a list of rows; each inner list supplies the fields for one CSV row. To write columns represented by parallel lists instead, combine them with zip() first.

How can I read a CSV file in Python?

Use csv.reader or csv.DictReader from the built-in csv module. If your project uses pandas, pandas.read_csv() can load a file into a DataFrame.

Can I append rows to an existing CSV file?

Yes. Open the file in append mode ("a") with newline="", create a csv.writer, and call writerow() or writerows() for the new records. Add a header only when the file does not already have one.