Hit enter after type your search item

2 Ways to Create List to CSV File in Python

How to create CSV file in Python

In this tutorial, we will show writing a list into a CSV file in Python programs.

  • The first way is using the Pandas library
  • Second way is by using the CSV module

First way – Using CSV to create a CSV file based on a list

For this program, we have created a list of employees for the demo only.

Step 1:

First of all, import the csv:

import csv

Step 2:

This is followed by creating a list of employees with Id, Employee Name, and Salary:

Step 3:

With statement is used where we used the open method and provided the CSV file name where we want to store the list.

If the file does not exist, a new file is created.

See the complete program below that uses writerows() method as follows:


As we executed the above program, a file namely test123.csv is created in the same directory where the Python code file exists.

As we open this file in the notepad, this is how it looks:


The same file in MS Excel:


Second way – using Pandas to write a list to a CSV file

In this Python program, we are using the Pandas library to save the list into a new CSV file.

Step 1:

Include Pandas in your program:

import pandas as pd

Step 2:

Create a list that you want to save in a file as CSV.

We are using the same list as used in the above program.

Step 3:

Specify list and column names in the Data frame:

df_emp = pd.DataFrame (emp_list, columns = [’emp_id’, ‘Employee Name’, ‘Salary’])

Step 4:

Use the data frame to_csv method:

df_emp.to_csv(‘csv_emp.csv’, index=False)

Complete program:


A new file emp_tst.csv should be generated in the same directory where the code file is located.

This div height required for enabling the sticky sidebar