How to Write a List to CSV in Python

by | Python

If you’ve been wrestling with Python lists and wondering how you can save them as a neat CSV file, you’re in the right place. One of the most common tasks in Python is to write a list to a comma-delimited file, and today, we’re going to turn you into a CSV file-conjuring pro!

To write a list to CSV in Python, you can use a built-in module called csv that’s designed especially for reading and writing CSV files. The module’s csv.writer class provides functions for writing simple and nested lists to CSV files.

Other methods to get the same result include installing and using the external Pandas or NumPy modules.

This article will explain the concepts and walk you step-by-step through the process with all the code examples you need to get started.

Let’s go!

What Are Lists in Python?

how to write a list to csv in python

A list in Python is a built-in data structure that is used to store multiple items in a single variable.

Python lists are:

  • ordered (starting from zero).

  • mutable (can be modified).

  • able to store duplicate values.

Lists are defined by having values between square brackets [] and items are separated by commas. The items in a list can be of any data type, such as numbers, strings, and even other lists.

Here is an example of creating a simple list:

products = [“t-shirts”, “hoodies”, “jeans”]

Lists of Lists in Python

Lists of lists are also known as nested lists. These are lists that contain other lists as their elements.

They’re often used in Python to represent structured data in rows and columns. They’re commonly used in scenarios where you need to group related data together.

Here is an example of creating a nested list that represents products, prices, and sales.

product_sales = [[“t-shirts”, 9.99, 342], [“hoodies”, 24.99, 118], [“jeans”, 29.99, 612]

CSV Files and the CSV Module in Python

CSV files represent tabular data in a simple file format. Each line in the file corresponds to a row in the table. Each value in the row (or line) is separated by a comma — this is why the format is called ‘Comma Separated Values.’

This is what a file representing products, prices, and sales could look like in CSV format:

  • t-shirts, 9.99, 342

  • hoodies, 24.99, 118

  • jeans, 29.99, 612

illustartion of csv file with three rows

The csv module is a built-in Python module that provides tools to read and write data in CSV format. It’s especially useful due to its simplicity and efficiency.

By importing the csv module, you gain access to the csv.writer() class, which allows you to create a writer object that can write a delimited string to a file object.

Alright, now that we’ve gone over the basics, let’s take a look at how you can write a simple list to CSV using Python in the next section.

How to Write a Simple List to CSV in Python

The first technique to learn is for a simple (non-nested) list file that looks like this:

  • t-shirts

  • hoodies

  • jeans

simple csv file with a list of single items

The broad steps to write the list to CSV file are:

  1. Import the csv module.

  2. Create a Python list.

  3. Open a CSV file in write mode.

  4. Use the writerow() function of the csv writer object.

  5. Save and close the CSV file.

The writerow() function will write multiple rows, i.e., all the data to the file. Here’s the code with detailed comments.

# Step 1: Importing the CSV module
import csv

# Step 2: Creating a Python list
my_list = ["t-shirts", "hoodies", "jeans"]

# Step 3: Opening a CSV file in write mode

with open('products.csv', 'w', newline='') as file:
    # Step 4: Using csv.writer to write the list to the CSV file
    writer = csv.writer(file)
    writer.writerow(my_list) # Use writerow for single list

# Step 5: The file is automatically closed when exiting the 'with' block

Using Context Managers

If you’re not familiar with the “with” method of working with files, this is a technique that uses a context manager that manages the file resources.

For example, when you use it to write CSV files, the files are automatically closed when the with statement ends.

How to Write a List of Lists to CSV

With a list of lists, each inner list should be a CSV row in the file. This is what the CSV data will look like:

traditional csv file with comma-separated values

The broad steps are similar to the previous section except that you use the writerows() function instead of writerow().

# Step 1: Importing the CSV module
import csv

# Step 2: Creating a Python list of lists
my_list = [["t-shirts", 9.99, 342], ["hoodies", 24.99, 118], ["jeans", 29.99, 612]]

# Step 3: Opening a CSV file in write mode
with open('product_sales.csv', 'w', newline='') as file:
    # Step 4: Using csv.writer to write the list to the CSV file
    writer = csv.writer(file)
    writer.writerows(my_list) # Use writerows for nested list

# Step 5: The file is automatically closed when exiting the 'with' bloc

In this second example, each inner list is written as a separate row in the file.

Note that the csv module lets you avoid having to enumerate or loop through the data structure and write it row by row.

Next, we’ll look at how you can append data to existing CSV files.

How to Append Data to Existing CSV Files

illustration of appending a new row to a csv file

To append data to an existing CSV file, follow these steps:

  1. Import the csv module.

  2. Open the CSV file in append mode (‘a’) using the with open() context manager.

  3. Create a new list containing data to be appended.

  4. Create a csv.writer() object by passing the opened file.

  5. Use the csv.writer() object to write the new list to the CSV file.

Here’s an example that writes adds a nested list (one row) to an existing file:

import csv

new_row = [["leggings", 22.99, 483]]

with open('product_sales.csv', 'a', newline='') as file:
    writer = csv.writer(file)
    writer.writerows(new_row)

Make sure to include newline=” when opening the file to ensure proper formatting of the CSV file on different platforms.

In the next section, we’ll cover how you can use the DictWriter class to write to CSV files.

How to Use DictWriter to Write to CSV Files

illustration of a dictionary

So far, we’ve learned how to write lists to CSV files. However, Python has other data structures like dictionaries that can also be written to a CSV file.

The DictWriter() class is also part of the csv module. It is similar to the writer() class but it handles dictionary data.

Here’s an example:

import csv

product_sales_dict = [
    {"product": "t-shirts", "price": 9.99, "sales": 342},
    {"product": "hoodies", "price": 24.99, "sales": 118},
    {"product": "jeans", "price": 29.99, "sales": 612}
]

fieldnames = ["product", "price", "sales"]

with open("dictionary.csv", "w", newline="") as csvfile:
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

    writer.writeheader()
    writer.writerows(product_sales_dict)

Advanced CSV Techniques in Python

Using the csv module isn’t the only method to export Python lists as CSV files.

Two popular libraries which provide functionality for this task are Pandas and NumPy.

1. How to Export Data With Pandas

illustration of pandas

Pandas is a widely used Python library for data manipulation and analysis.

To save Python lists as a CSV file, you can create a pandas DataFrame from the list and then use the to_csv() method, as shown below:

import pandas as pd
# Example list of lists
data = [['ABC', 1.2, 30], ['XYZ', 0.8, 20], ['LMN', 2.3, 50]]
# Create a DataFrame from the list
data_frame = pd.DataFrame(data, columns=['Name', 'Value', 'Quantity'])
# Save DataFrame to CSV file
data_frame.to_csv('output.csv', index=False, encoding='utf-8')

If your data includes non-ASCII characters, you may need to set the encoding parameter to an appropriate value, such as ‘utf-8.’

2. How to Use NumPy to Save Lists to CSV

Another library worth considering is NumPy which provides support for working with arrays and matrices.

NumPy offers the np.savetxt() function as a convenient method to save data to a CSV file.

Here’s an example showcasing how to save a list to a CSV file using NumPy:

import numpy as np
# Example list of lists
data = [['ABC', 1.2, 30], ['XYZ', 0.8, 20], ['LMN', 2.3, 50]]
# Save the list to a CSV file
np.savetxt('output.csv', data, delimiter=',', fmt='%s', header='Name,Value,Quantity', comments='')

Here’s a breakdown of the savetxt() parameters:

  • delimiter: specifies the field separator

  • fmt parameter: indicates the format of the data being saved

  • %s: the placeholder for string values

  • header: a list of column headers

  • comments: control the characters that begin comments in the output file.

How to Clean Data Before Writing to File

It’s a good idea to clean up your data before writing it to text files. This video will give you the techniques you need:

Final Thoughts

Writing a list to a CSV file in Python is a simple and effective way to store and share data. You’ve learned how to use Python’s built-in csv module to perform this task.

You are now familiar with creating a Python list, importing the csv module, opening a CSV file, and using the csv.writer to write the list to the CSV file.

You also learned the more complex scenarios of writing a list of lists to a CSV file, which allows you to store tabular data.

Don’t forget, like all things in programming, practice makes perfect. So, take this newfound knowledge, play around with it, and see how it works with different data sets and project needs.

This is just the tip of the Python iceberg. There’s so much more to explore in the vast world of Python, so don’t stop here. But for today, give yourself a pat on the back. You’ve successfully tamed the beast of list-to-CSV conversion!

author avatar
Sam McKay, CFA
Sam is Enterprise DNA's CEO & Founder. He helps individuals and organizations develop data driven cultures and create enterprise value by delivering business intelligence training and education.

Related Posts