Convert List to CSV File & Vice Versa in Python (Examples)
Hi! This tutorial will show you how to transform a list to a csv file and vice-versa in the Python programming language.
First, though, here is an overview of this tutorial:
Let’s dive into Python code!
Create Sample List
Here, we will create the sample list that we will turn into a csv file in this tutorial. Therefore, in your Python programming IDE, run the line of code below to create the sample list:
my_list = [["name", "age", "gender"], ["John", "25", "male"], ["Emily", "30", "female"], ["Mike", "40", "male"]] print(type(my_list)) # <class 'list'>
As displayed by the type() function, we created an object with the list type. Let’s now pass on the examples!
Example 1: List to CSV | Turn List into CSV File Using csv.writer() Function
In this first example, we will use the
csv.writer() function from the csv module to turn the list into a csv file:
with open('my_list.csv', 'w', newline='') as file: writer = csv.writer(file) for row in my_list: writer.writerow(row)
In the above code, we first opened a file and specified the file name as “my_list.csv”. We then created a writer object with the
csv.writer() function, and parsed the file object. Thereafter, we looped through each row in the list and used the
writerow() function to write each row to the file.
The written csv file should then be inside your working directory. Please check it first. If you are not sure what your current working directory is, you can use the
getcwd() function of the os module.
Example 2: List to CSV | Turn List into CSV File Using pandas to_csv() Method
In this second example, we will use pandas
to_csv() method to turn the list into a csv file. First, though, we need to install and import pandas:
# install pandas pip install pandas # import pandas import pandas as pd
Next, we will convert the list into a DataFrame, then a csv file as follows.
df = pd.DataFrame(my_list[1:], columns=my_list) df.to_csv('my_list.csv', index=False)
In the code above, we create a pandas DataFrame object and parse the list to it, with the first row as the column headers. We then use the
to_csv() method to save it to a file named “my_list.csv”. We set the argument
index = to “False” to prevent the row index from being written to the file.
You can then check your working directory, wherein the csv file must now be saved. Next, we will demonstrate how to convert the csv file back into a Python list.
Example 1: CSV to List | Turn CSV File to List Using csv.reader() Function
In this example, we will use the
csv.reader() function from the csv module to transform the csv file into a Python list:
new_list =  with open("my_list.csv", "r") as file: reader = csv.reader(file) for row in reader: new_list.append(row) print(new_list) # [['name', 'age', 'gender'], ['John', '25', 'male'], ['Emily', '30', 'female'], ['Mike', '40', 'male']] print(type(new_list)) # <class 'list'>
We first opened the file in read mode
"r"; then used the
csv.reader() function to read the csv file. We then iterated through each row in the reader object, and appended rows to the list object new_list.
Example 2: CSV to List | Turn CSV File to List Using pandas tolist() Method
In this second example, we will use pandas
tolist() method to convert the csv file into a list:
df = pd.read_csv("my_lists.csv") new_list = df.values.tolist() header = df.columns.tolist() new_list.insert(0, header) print(new_list) # [['name', 'age', 'gender'], ['John', '25', 'male'], ['Emily', '30', 'female'], ['Mike', '40', 'male']] print(type(new_list)) # <class 'list'>
In the code above, we, first read the csv file; then we converted its values into a list with the
df.values.tolist() method. This will return a list of values but will not include the column headers of the csv file in the list.
To include the column headers in the list, we first used the
df.columns.tolist() function to create the list of column headers, and then insert it into the “new_list” object with the
new_list.insert() function, wherein we specified that we want the list of column headers to be at index position 0.
With that, we have demonstrated how to convert a list into a csv file and vice-versa in Python. I hope you found this tutorial helpful!
Video, Further Resources & Summary
Do you need more explanations on how to convert a list into a csv file and vice-versa in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
In the video, we explain in some more detail how to convert a list into a csv file and vice-versa in Python.
The YouTube video will be added soon.
Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:
- Check if Linked List is Empty in Python (2 Examples)
- Convert List to pandas DataFrame in Python (3 Examples)
- Learn Python Programming
- Convert List to Range in Python (2 Examples)
- Convert List to Dictionary in Python (3 Examples)
This post has shown how to convert a list into a csv file and vice-versa in Python. In case you have further questions, you may leave a comment below.
This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.
Statistics Globe Newsletter