Create CSV file from List of Dictionaries in Python

In this article, we will discuss how to create a csv file from a list of dictionaries in Python.

Table Of Contents

Introduction to Problem

Suppose we have a list of dictionaries. Like this,

# List of dictionaries
listOfDict = [
    {'Name' : 'Ritika', 'Age' : 27, 'City' : 'Delhi',     'Country' : 'India'},
    {'Name' : 'Mark',   'Age' : 28, 'City' : 'Sydney',    'Country' : 'India'},
    {'Name' : 'Suse',   'Age' : 29, 'City' : 'Las Vegas', 'Country' : 'India'},
    {'Name' : 'Shaun',  'Age' : 30, 'City' : 'London',    'Country' : 'India'} ]

Now, we want to create a csv file from this list of dictionaries. Keys of the dictionaries should be the column names in the csv file. Whereas, values of each dictionary should be a row in the csv file. Contents of csv file should be like this,

Name,Age,City,Country
Ritika,27,Delhi,India
Mark,28,Sydney,India
Suse,29,Las Vegas,India
Shaun,30,London,India

Let’s see how to do this.

Advertisements

Solution

Open the csv file in write mode, and get a file object. Pass the file object & list of column names to DictWriter() function of csv module, to get the DictWriter object. Then call the writeheader() function of DictWriter object to add the header in the csv file. Then call the writerows() function of DictWriter object with the list of dictionaries. It will add the values of each dictionary as a row in the csv file.

Let’s see an example,

import csv

# List of dictionaries
listOfDict = [
    {'Name' : 'Ritika', 'Age' : 27, 'City' : 'Delhi',     'Country' : 'India'},
    {'Name' : 'Mark',   'Age' : 28, 'City' : 'Sydney',    'Country' : 'India'},
    {'Name' : 'Suse',   'Age' : 29, 'City' : 'Las Vegas', 'Country' : 'India'},
    {'Name' : 'Shaun',  'Age' : 30, 'City' : 'London',    'Country' : 'India'} ]

# Open the csv file in write mode
with open('employees.csv', 'w') as fileObject:
    # Get a DictWriter object
    writerObj = csv.DictWriter(
                        fileObject,
                        fieldnames = listOfDict[0].keys())
    # Add Header to csv file
    writerObj.writeheader()
    # Add rows in the csv file
    writerObj.writerows(listOfDict)

It will create a csv file employees.csv with the following contents,

Name,Age,City,Country
Ritika,27,Delhi,India
Mark,28,Sydney,India
Suse,29,Las Vegas,India
Shaun,30,London,India

Summary

We learned how to create csv file from a list of dictionaries in Python. Thanks.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top