Convert CSV to List in Python

In this post, we are going to learn how to convert CSV to a list in Python with examples. The Python CSV module is used to read and write CSV files. We will use the CSV module as well Python machine learning panda’s module to convert Convert CSV to List in Python which includes many examples Convert CSV to List with a header using CSV or Pandas Module, Convert CSV to list of dictionaries, convert csv to list of the tuple, Convert csv to list without header and Convert CSV to list without any module.

1. Convert CSV to list without any module


We have used the python inbuilt function open() to open the CSV file in reading mode “r”.The list comprehension to iterate over each row of a csv file and rstrip() method to remove (\n) at end of each line. This is how we read a csv file into the list without any module in Python.

with open('data.csv', 'r') as file:
    
 lines = [line.rstrip() for line in file]
print(lines)

Output

[Name,Marks,Address,Subject', 'Tony,98,Chicago,Math', 'Jack,100,US,English', 'John,100,India,Music', 'Rim,100,Canda,Chemistry']

2. Convert CSV to List in Python


To read a csv to list First we have to import the CSV module using “import csv” and used its open() method to open the CSV file in reading mode. The CSV class reader() method to read all contents of the file and Finally, the list() method to convert the content of the file to a list this is how we read CSV files into a list of lists with headers.

import csv

with open('data.csv', 'r') as File:

 content= csv.reader(File)

 CSV_toList = list(content)

 print(CSV_toList)

Output

[['Name', 'Marks', 'Address', 'Subject'], ['Tony', '98', 'Chicago', 'Math'], ['Jack', '100', 'US', 'English'], ['John', '100', 'India', 'Music'], ['Rim', '100', 'Canda', 'Chemistry']]

3. Pandas convert CSV to list with header


Another way to read a CSV file with headers into a list of lists is by using the Pandas module with a delimiter comma(,). We have converted the CSV file into Pandas dataframe by using the read_csv() method.

We have read csv file contents by using dfobj. values. and used List comprehension to iterate over the rows of CSV files and convert them into a list. The dfobj.columns to get all columns of the CSV file and to_list() method to convert them into the list. The list. insert() method to used to insert all columns at 0 indexes in the list.

import pandas as pd

dfobj = pd.read_csv('data.csv', delimiter=',')

csv_to_list = [list(row) for row in dfobj.values]

csv_to_list.insert(0, dfobj.columns.to_list())

print(csv_to_list)

Output

[['Name', 'Marks', 'Address', 'Subject'], ['Tony', 98, 'Chicago', 'Math'], ['Jack', 100, 'US', 'English'], ['John', 100, 'India', 'Music'], ['Rim', 100, 'Canda', 'Chemistry']]

4. CSV to List of dictionaries in Python


In this python program, we have used the CSV module DictReader class to convert a csv file into a list of dictionaries.

  • Import the DictReader class from the CSV module.
  • open the file in reading mode
  • The list() to convert file contents into a list of dictionary
from csv import DictReader

with open('data.csv', 'r') as FileObj:
    
    dictReader = DictReader(FileObj)
    
    CSV_To_listofdict = list(dictReader)
   
    print(CSV_To_listofdict)

Output

[{Name': 'Tony', 'Marks': '98', 'Address': 'Chicago', 'Subject': 'Math'}, {Name': 'Jack', 'Marks': '100', 'Address': 'US', 'Subject': 'English'}, {Name': 'John', 'Marks': '100', 'Address': 'India', 'Subject': 'Music'}, {Name': 'Rim', 'Marks': '100', 'Address': 'Canda', 'Subject': 'Chemistry'}]

5. Convert CSV to List using Pandas


In this Python program example, we have used the Python Pandas module to read a CSV file into a list, The pd.read_csv() to read csv into the pandas dataframe.The dfobj. values] to get all contents of CSV file and list comprehension to read row by row convert into the list.

import pandas as pd

dfobj = pd.read_csv('data.csv', delimiter=',')
csv_to_list = [list(row) for row in dfobj.values]
print(csv_to_list)

Output

[['Tony', 98, 'Chicago', 'Math'], ['Jack', 100, 'US', 'English'], ['John', 100, 'India', 'Music'], ['Rim', 100, 'Canda', 'Chemistry']]

6. Convert CSV to list of tuples


In this program, the open(‘data.csv’, ‘r’) is used to open file in read mode ‘r’ and csv.reader() method to read file contents.The list(map(tuple, reader)) code is used to iterate over each row of csv file convert it into list of tuples.

import csv

with open('data.csv', 'r') as file:

 reader = csv.reader(file)

 CSV_toListoftuple = list(map(tuple, reader))

 print(CSV_toListoftuple)

Output

[(‘Name’, ‘Marks’, ‘Address’, ‘Subject’), (‘Tony’, ’98’, ‘Chicago’, ‘Math’), (‘Jack’, ‘100’, ‘US’, ‘English’), (‘John’, ‘100’, ‘India’, ‘Music’), (‘Rim’, ‘100’, ‘Canda’, ‘Chemistry’)]

Summary

In this post, we have learned how to Convert CSV to List in Python with examples by using CSV and Pandas Module.