Last modified: Jan 26, 2026 By Alexander Williams
Python List of Lists to CSV Export Guide
Python lists are versatile. They can store any data type. A list of lists is a common structure. It often represents tabular data. Think of rows and columns.
You might have data from a web scraper. Or results from a calculation. Storing this data is crucial. The CSV format is perfect for this.
CSV stands for Comma-Separated Values. It is a simple file format. Many programs can read it. Excel, Google Sheets, and databases use it.
This guide shows you how to convert a list of lists to a CSV file. We will use Python's built-in csv module. It is powerful and easy to use.
Understanding a List of Lists
A list of lists is a nested list. Each inner list can be a row of data. The elements in the inner list are the columns.
Here is a simple example. It represents employee data.
# A list of lists representing employee data
employees = [
["Name", "Department", "Salary"], # Header row
["Alice", "Engineering", 75000],
["Bob", "Marketing", 65000],
["Charlie", "Sales", 72000]
]
This structure is ideal for CSV export. The first list can be the header. The following lists are the data rows. For more on building these structures, see our guide on Python List in List Append.
Writing to CSV with csv.writer
The primary tool is the csv.writer object. You create it with a file object. Then you write rows using its methods.
Let's export our employee data.
import csv
employees = [
["Name", "Department", "Salary"],
["Alice", "Engineering", 75000],
["Bob", "Marketing", 65000],
["Charlie", "Sales", 72000]
]
# Open a file for writing
with open('employees.csv', 'w', newline='') as file:
writer = csv.writer(file)
# Write all rows at once
writer.writerows(employees)
print("File 'employees.csv' created successfully.")
File 'employees.csv' created successfully.
The newline='' argument is important. It prevents blank rows on some operating systems. The writer.writerows() method takes the entire list.
You can also write rows one by one with writer.writerow(). This is useful for streaming data.
Reading the CSV File Back
To verify your work, read the file back. Use the csv.reader object. It returns an iterator.
import csv
# Open the file for reading
with open('employees.csv', 'r', newline='') as file:
reader = csv.reader(file)
# Convert the reader object to a list
data_from_csv = list(reader)
print("Data read from CSV:")
for row in data_from_csv:
print(row)
Data read from CSV:
['Name', 'Department', 'Salary']
['Alice', 'Engineering', '75000']
['Bob', 'Marketing', '65000']
['Charlie', 'Sales', '72000']
Notice the numbers are now strings. CSV files store all data as text. You must convert types when reading if needed.
Customizing the CSV Format
The basic writer uses commas. You can change the delimiter. Use a tab or a semicolon.
Create a writer with the delimiter parameter.
import csv
data = [["A", "B", "C"], [1, 2, 3], [4, 5, 6]]
with open('data.tsv', 'w', newline='') as file:
# Use tab as delimiter for TSV file
writer = csv.writer(file, delimiter='\t')
writer.writerows(data)
You can also add quoting for text fields. Use the quoting parameter. This helps if your data contains commas.
Working with Dictionaries: csv.DictWriter
Often, data is more clear as a list of dictionaries. Each dictionary is a row. Keys are column headers.
The csv.DictWriter class is perfect for this. You must specify the fieldnames first.
import csv
employees_dict = [
{"Name": "Alice", "Department": "Engineering", "Salary": 75000},
{"Name": "Bob", "Department": "Marketing", "Salary": 65000},
{"Name": "Charlie", "Department": "Sales", "Salary": 72000}
]
fieldnames = ["Name", "Department", "Salary"]
with open('employees_dict.csv', 'w', newline='') as file:
writer = csv.DictWriter(file, fieldnames=fieldnames)
writer.writeheader() # Write the header row from fieldnames
writer.writerows(employees_dict)
This method is robust. It ensures column order. It's great for data with many columns. To learn more about converting between structures, read Python List to Dict Conversion.
Handling Headers Separately
Sometimes your data list doesn't include headers. You can add them during writing.
Write the header row first. Then write the data.
import csv
# Data without headers
data_rows = [
["Alice", "Engineering", 75000],
["Bob", "Marketing", 65000]
]
headers = ["Name", "Department", "Salary"]
with open('data_with_headers.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(headers) # Write header
writer.writerows(data_rows) # Write data
Common Issues and Solutions
Missing newline='': This can cause blank lines in the CSV on Windows. Always include it.
Data with Commas: If your strings contain commas, they will break the format. Use csv.writer with quoting=csv.QUOTE_ALL to wrap all fields in quotes.
import csv
problem_data = [["City", "Description"], ["New York", "Big, vibrant city"]]
with open('quoted.csv', 'w', newline='') as file:
writer = csv.writer(file, quoting=csv.QUOTE_ALL)
writer.writerows(problem_data)
Inconsistent Row Length: All inner lists should have the same Python List Length. The CSV writer will still write them, but it may cause errors when reading. Check lengths first.
Practical Example: Exporting Analysis Results
Imagine you analyzed a list of numbers. You found the maximum and sum. You want to export the results.
import csv
# Sample data
numbers = [45, 23, 67, 12, 89, 34]
# Perform some analysis
max_value = max(numbers)
sum_value = sum(numbers)
# Prepare a list of lists for results
results = [
["Metric", "Value"],
["Maximum", max_value],
["Sum", sum_value]
]
with open('analysis_results.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerows(results)
print("Analysis results exported.")
For more on finding maximums, see Find Maximum in Python List.
Conclusion
Exporting a list of lists to CSV is a fundamental Python skill. The csv module makes it simple.
Remember the key steps. Open a file with newline=''. Create a csv.writer object. Use writerows() for your data.
For dictionary data, use csv.DictWriter. It handles headers neatly.
This process is essential for data sharing. It works with spreadsheets, databases, and other tools. Start exporting your Python data today.