In this Python tutorial, we will learn the Python write list to CSV. I will show 2 different examples explaining, how to write a list to a CSV file in Python.
Python write a list to CSV
Here, we can see how to write a list to CSV in Python.
- In this example, I have imported a module called csv and declared a variable as details.
- And another variable called rows as rows = [ [‘sushma’, ‘2nd’, ‘2023’, ‘Physics’], [‘john’, ‘3rd’, ‘2022’, ‘M2’], [‘kushi’, ‘4th’, ‘2021’, ‘M4’]] and then to open the csv file, I have used with open(‘student.csv’, ‘w’) as f: is used to open the file.
- The student.csv is the name of the file, the “w” mode is used to write the file, to write the list to the CSV file write = csv.writer(f), to write each row of the list to csv file writer.writerow() is used.
Example:
import csv
Details = ['Name', 'class', 'passoutYear', 'subject']
rows = [ ['sushma', '2nd', '2023', 'Physics'], ['john', '3rd', '2022', 'M2'], ['kushi', '4th', '2021', 'M4']]
with open('student.csv', 'w') as f:
write = csv.writer(f)
write.writerow(Details)
write.writerows(rows)
You can see the list as the output in the below screenshot.
Let us check another example.
Write a list to CSV in Python
Python has built-in support for writing to and reading from CSV files through its CSV module. Here is a step-by-step guide on how to write a list to a CSV file.
Step 1: Create Your List
First, create a list or lists that you want to write to a CSV file in Python.
# Example list
data = ['apple', 'banana', 'cherry']
# Example list of lists (represents rows and columns)
data_matrix = [
['Name', 'Age', 'Country'],
['Alice', 30, 'USA'],
['Bob', 28, 'Canada'],
['Charlie', 35, 'UK']
]
Step 2: Write List to CSV
Now, let’s write the list to a CSV file using the csv module.
Example 1: Writing a Single List
In this example, the data list will be written to a CSV file where each element is in a new row.
import csv
data = ['apple', 'banana', 'cherry']
# Open file to write data
with open('fruits.csv', 'w', newline='') as file:
writer = csv.writer(file)
# Write each item in a new row
for item in data:
writer.writerow([item])
Read How to remove duplicate elements from a List in Python
Example 2: Writing a List of Lists (Matrix)
In this example, the data_matrix list will be written to a CSV file where each inner list represents a row.
import csv
data_matrix = [
['Name', 'Age', 'Country'],
['Alice', 30, 'USA'],
['Bob', 28, 'Canada'],
['Charlie', 35, 'UK']
]
# Open file to write data
with open('people.csv', 'w', newline='') as file:
writer = csv.writer(file)
# Write each inner list as a new row
writer.writerows(data_matrix)
- In the open() function, the ‘w’ parameter specifies that the file should be opened in write mode.
- The newline=” parameter in the open() function ensures that newlines are handled properly on all platforms.
- The writerow() method writes a single row to the CSV file. It takes an iterable (like a list) as an argument.
- The writerows() method writes multiple rows at once. It takes a list of iterables.
This is how to write lists to CSV files in Python.
You may also like:
- Write String to a .csv File in Python
- How to read a CSV file in Python
- How to save Python dictionary to CSV
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.