- How To Read CSV to List in Python
- What is CSV File?
- Reading CSV files into List in Python.
- Example: Reading CSV to List in Python
- Example 2: Selection of data by using row and column numbers
- Example 3: Using Pandas to read CSV
- Example 4: Read CSV files into a list of tuples using Python
- Example 5: Read CSV into a list of dictionaries using Python
- Conclusion
- How to Import CSV to List Python
- Example_1: Convert the CSV to List in Python
- Output:
- Example_2: Using Pandas to Read CSV List
- Output:
- Example_3: Convert the CSV File Data into a List of Tuples
- Output:
- Example_4: Convert the CSV file data into a list of dictionaries
- Output:
- Example_5: Using the Pandas to Convert the CSV File Data into a List with the Header
- Output:
- Conclusion
How To Read CSV to List in Python
In this tutorial, we will learn how to read CSV to list in python. First, let’s discuss the CSV.
What is CSV File?
- CSV (Comma Separated Values) is a plain text file. These CSV files are used in different applications for exchanging data like contact managers and databases.
- These CSV files are also called Comma Delimited Files or Character Separated Values.
- The different names are given to CSV files since we can export complex data from one application to a CSV file and then we can import data from these CSV file data into another application.
Reading CSV files into List in Python.
- We can read the CSV files into different data structures like a list, a list of tuples, or a list of dictionaries.
- We can use other modules like pandas which are mostly used in ML applications and cover scenarios for importing CSV contents to list with or without headers.
Example: Reading CSV to List in Python
This is a sample CSV file that will be used to read into a list.
Id, Name, Course, City, Session 1, Bheem, Python, India, Morning 2, Chutki, Python, London, Evening 3, Tom, Python, USA, Morning 4, Jerry, Python, Japan, Morning
- Now we have to read this file into a list of lists in python.
- Initially, Import CSV to a list of lists using CSV. reader.
- Python has a built-in CSV module, it will help to read the data from the CSV file using a reader class. i.e, from CSV import reader.
import csv with open('students.csv', 'r') as read_obj: # read csv file as a list of lists csv_reader = csv.reader(read_obj) # pass the file object to reader() to get the reader object list_of_rows = list(csv_reader) # Pass reader object to list() to get a list of lists print(list_of_rows)
[[‘Id’, ‘Name’, ‘Course’, ‘Country’, ‘Session’], [‘1’, ‘Bheem’, ‘Python’, ‘India’, ‘Morning’],
[‘2’, ‘Chutki’, ‘Python’, ‘London’, ‘Evening’],
[‘3’, ‘Tom’, ‘Python’, ‘USA’, ‘Morning’],
[‘4’, ‘Jerry’, ‘Python’, ‘Japan’, ‘Morning’]]
- It created a list of lists containing all rows of the CSV file and print that list of lists.
- Here, in the first step, the file is read to be open, so open the file in reading mode and later transfer that file object into the function csv_reader().
- It will result in an iterator, which can be used to iterate over all the lines of the CSV file.
- We want the result in list format so the list() function is used to return the result in the list of the list.
- The obtained result row of CSV and each item in the list represents a cell/column in that row.
Example 2: Selection of data by using row and column numbers
By using a list of lists created above we can select individual data by using row and column numbers.
row_number = 2 col_number = 1 value = list_of_rows[row_number - 1][col_number - 1] print('Value in a cell at 2nd row and 1st column : ', value
Value in a cell at 2nd row and 1st column: 1
Example 3: Using Pandas to read CSV
The below example shows how to read the CSV file into a list without the header by using the pandas library.
import pandas as pd df = pd.read_csv('students.csv', delimiter=',') list_of_rows = [list(row) for row in df.values] print(list_of_rows)
[‘1’, ‘Bheem’, ‘Python’, ‘India’, ‘Morning’], [‘2’, ‘Chutki’, ‘Python’, ‘London’, ‘Evening’],
[‘3’, ‘Tom’, ‘Python’, ‘USA’, ‘Morning’],
[‘4’, ‘Jerry’, ‘Python’, ‘Japan’, ‘Morning’]]
- Here, first, upload the CSV file into a data frame using read_csv().
- Dataframe values return all rows in 2d Numpy format excluding the header.
- Then we iterated over all rows of this obtained result using list comprehension and created a list of lists.
Example 4: Read CSV files into a list of tuples using Python
- First upload data from the above CSV file that is Student.csv into a list of tuples, where each tuple in the list represents a row and each data in the tuple represents a cell.
with open('students.csv', 'r') as read_obj: # pass the file object to reader() to get the reader object csv_reader = reader(read_obj) # Get all rows of csv from csv_reader object as list of tuples list_of_tuples = list(map(tuple, csv_reader)) # display all rows of csv print(list_of_tuples)
[(‘Id’, ‘Name’, ‘Course’, ‘Country’, ‘Session’), (‘1’, ‘Bheem’, ‘Python’, ‘India’, ‘Morning’),
(‘2’, ‘Chutki’, ‘Python’, ‘London’, ‘Evening’),
(‘3’, ‘Tom’, ‘Python’, ‘USA’, ‘Morning’),
(4′, ‘Jerry’, ‘Python’, ‘Japan’, ‘Morning’)]
Example 5: Read CSV into a list of dictionaries using Python
By using the DictReader module, we can read CSV into a list of dictionaries.
from CSV import DictReader # open file in the read mode with open('students.csv', 'r') as read_obj: # pass the file object to DictReader() to get the DictReader object dict_reader = DictReader(read_obj) # get a list of dictionaries from dct_reader list_of_dict = list(dict_reader) # print list of dict i.e. rows print(list_of_dict)
Conclusion
In this tutorial, we learned how to read a CSV file to List from different approaches.
How to Import CSV to List Python
A CSV is a (comma separated values) file in which data is in the form of a tabular. The extension of the CSV file is .csv. This csv file is mostly used in the data analytics. Apart from the data analytics, the CSV file also used in the e-commerce application because it’s very easy to handle in all different types of programming languages.
We can convert the CSV to different data structures like a list, a list of tuples and a list of dictionaries. We can also save the CSV without the header or with the header as a list, and for that we can use some machine learning libraries like Pandas.
Example_1: Convert the CSV to List in Python
The below is a CSV sample file which will be used to convert into a list.
with open ( ‘sample.csv’ , ‘r’ ) as read_obj:
csv_reader = csv . reader ( read_obj )
list_of_csv = list ( csv_reader )
Output:
[ [ ‘JAN’ , 340 , 360 , 417 ] , [ ‘FEB’ , 318 , 342 , 391 ] , [ ‘MAR’ , 362 , 406 , 419 ] , [ ‘APR’ , 348 , 396 , 461 ] , [ ‘MAY’ , 363 , 420 , 472 ] , [ ‘JUN’ , 435 , 472 , 535 ] , [ ‘JUL’ , 491 , 548 , 622 ] , [ ‘AUG’ , 505 , 559 , 606 ] , [ ‘SEP’ , 404 , 463 , 508 ] , [ ‘OCT’ , 359 , 407 , 461 ] , [ ‘NOV’ , 310 , 362 , 390 ] , [ ‘DEC’ , 337 , 405 , 432 ] ]Line 1: We import the CSV module.
Line 2 to 4: We open the sample.csv file in the read mode ‘r’. Then we pass the read_obj to the csv.reader() method while creating an object to read the CSV file. Then we convert explicitly the CSV read data into a list using type cast.
Line 6: The output above shows that our CSV data is now successfully converted into the list.
Example_2: Using Pandas to Read CSV List
In this example, we are going to use the Pandas library to read the CSV file and convert them into a list. The CSV file is same which we have used in the example_1 (sample.csv).
df = pd. read_csv ( ‘sample.csv’ , delimiter = ‘,’ )
list_of_csv = [ list ( row ) for row in df. values ]
Output:
[ [ ‘JAN’ , 340 , 360 , 417 ] , [ ‘FEB’ , 318 , 342 , 391 ] , [ ‘MAR’ , 362 , 406 , 419 ] , [ ‘APR’ , 348 , 396 , 461 ] , [ ‘MAY’ , 363 , 420 , 472 ] , [ ‘JUN’ , 435 , 472 , 535 ] , [ ‘JUL’ , 491 , 548 , 622 ] , [ ‘AUG’ , 505 , 559 , 606 ] , [ ‘SEP’ , 404 , 463 , 508 ] , [ ‘OCT’ , 359 , 407 , 461 ] , [ ‘NOV’ , 310 , 362 , 390 ] , [ ‘DEC’ , 337 , 405 , 432 ] ]Line 1: We import the Pandas module as pd.
Line 2 to 3: We read the CSV file using the Pandas library read_csv and converted it into a dataframe (df). Then, we convert each row into a list and assign the result to the list_of_csv variable.
Line 4: The output above shows that our CSV data is now successfully converted into the list.
Example_3: Convert the CSV File Data into a List of Tuples
In this example, we are going to convert the CSV file data into a list of tuples. The CSV file is same which we have used in the example_1 (sample.csv).
with open ( ‘sample.csv’ , ‘r’ ) as read_obj:
csv_reader = csv . reader ( read_obj )
list_of_csv = list ( map ( tuple , csv_reader ) )
Output:
[ ( ‘Month’ , ‘ «1958»‘ , ‘ «1959»‘ , ‘ «1960»‘ ) , ( ‘JAN’ , ‘ 340’ , ‘ 360’ , ‘ 417’ ) , ( ‘FEB’ , ‘ 318’ , ‘ 342’ , ‘ 391’ ) , ( ‘MAR’ , ‘ 362’ , ‘ 406’ , ‘ 419’ ) , ( ‘APR’ , ‘ 348’ , ‘ 396’ , ‘ 461’ ) , ( ‘MAY’ , ‘ 363’ , ‘ 420’ , ‘ 472’ ) , ( ‘JUN’ , ‘ 435’ , ‘ 472’ , ‘ 535’ ) , ( ‘JUL’ , ‘ 491’ , ‘ 548’ , ‘ 622’ ) , ( ‘AUG’ , ‘ 505’ , ‘ 559’ , ‘ 606’ ) , ( ‘SEP’ , ‘ 404’ , ‘ 463’ , ‘ 508’ ) , ( ‘OCT’ , ‘ 359’ , ‘ 407’ , ‘ 461’ ) , ( ‘NOV’ , ‘ 310’ , ‘ 362’ , ‘ 390’ ) , ( ‘DEC’ , ‘ 337’ , ‘ 405’ , ‘ 432’ ) ]Line 1: We import the CSV module.
Line 2 to 4: We open the sample.csv file in the read mode ‘r’. We pass the read_obj to the csv.reader() method while creating an object to read the csv file. Then, we convert each row of the CSV into a tuple using a map function and at last convert the whole data into a list.
Line 5: The output above shows that our CSV data is now successfully converted into a list of tuples.
Example_4: Convert the CSV file data into a list of dictionaries
In this example, we are going to convert the CSV file data into a list of dictionaries. The CSV file is same which we have used in the example_1 (sample.csv).
with open ( ‘sample.csv’ , ‘r’ ) as read_obj:
dict_reader = csv . DictReader ( read_obj )
list_of_dict = list ( dict_reader )
Output:
[ { ‘Month’ : ‘JAN’ , ‘ «1958»‘ : ‘ 340’ , ‘ «1959»‘ : ‘ 360’ , ‘ «1960»‘ : ‘ 417’ } , { ‘Month’ : ‘FEB’ , ‘ «1958»‘ : ‘ 318’ , ‘ «1959»‘ : ‘ 342’ , ‘ «1960»‘ : ‘ 391’ } , { ‘Month’ : ‘MAR’ , ‘ «1958»‘ : ‘ 362’ , ‘ «1959»‘ : ‘ 406’ , ‘ «1960»‘ : ‘ 419’ } , { ‘Month’ : ‘APR’ , ‘ «1958»‘ : ‘ 348’ , ‘ «1959»‘ : ‘ 396’ , ‘ «1960»‘ : ‘ 461’ } , { ‘Month’ : ‘MAY’ , ‘ «1958»‘ : ‘ 363’ , ‘ «1959»‘ : ‘ 420’ , ‘ «1960»‘ : ‘ 472’ } , { ‘Month’ : ‘JUN’ , ‘ «1958»‘ : ‘ 435’ , ‘ «1959»‘ : ‘ 472’ , ‘ «1960»‘ : ‘ 535’ } , { ‘Month’ : ‘JUL’ , ‘ «1958»‘ : ‘ 491’ , ‘ «1959»‘ : ‘ 548’ , ‘ «1960»‘ : ‘ 622’ } , { ‘Month’ : ‘AUG’ , ‘ «1958»‘ : ‘ 505’ , ‘ «1959»‘ : ‘ 559’ , ‘ «1960»‘ : ‘ 606’ } , { ‘Month’ : ‘SEP’ , ‘ «1958»‘ : ‘ 404’ , ‘ «1959»‘ : ‘ 463’ , ‘ «1960»‘ : ‘ 508’ } , { ‘Month’ : ‘OCT’ , ‘ «1958»‘ : ‘ 359’ , ‘ «1959»‘ : ‘ 407’ , ‘ «1960»‘ : ‘ 461’ } , { ‘Month’ : ‘NOV’ , ‘ «1958»‘ : ‘ 310’ , ‘ «1959»‘ : ‘ 362’ , ‘ «1960»‘ : ‘ 390’ } , { ‘Month’ : ‘DEC’ , ‘ «1958»‘ : ‘ 337’ , ‘ «1959»‘ : ‘ 405’ , ‘ «1960»‘ : ‘ 432’ } ]Line 1: We import the CSV module.
Line 2 to 4: We open the sample.csv file in the read mode ‘r’. Then, we pass the read_obj to the
csv.DictReader method while creating an object to read the csv file. The csv.DictReader automatically converts each row into a dictionary. And then we convert the whole results into a list.
Line 6: The output above shows that our CSV data is now successfully converted into a list of dictionaries.
Example_5: Using the Pandas to Convert the CSV File Data into a List with the Header
In this example, we are going to use the Pandas library to read the csv file and convert them into a list along with header. The CSV file is same which we have used in the example_1 (sample.csv).
df = pd. read_csv ( ‘sample.csv’ , delimiter = ‘,’ )
list_of_csv = [ list ( row ) for row in df. values ]
list_of_csv. insert ( 0 , df. columns . to_list ( ) )
Output:
[ [ ‘Month’ , ‘ «1958»‘ , ‘ «1959»‘ , ‘ «1960»‘ ] , [ ‘JAN’ , 340 , 360 , 417 ] , [ ‘FEB’ , 318 , 342 , 391 ] , [ ‘MAR’ , 362 , 406 , 419 ] , [ ‘APR’ , 348 , 396 , 461 ] , [ ‘MAY’ , 363 , 420 , 472 ] , [ ‘JUN’ , 435 , 472 , 535 ] , [ ‘JUL’ , 491 , 548 , 622 ] , [ ‘AUG’ , 505 , 559 , 606 ] , [ ‘SEP’ , 404 , 463 , 508 ] , [ ‘OCT’ , 359 , 407 , 461 ] , [ ‘NOV’ , 310 , 362 , 390 ] , [ ‘DEC’ , 337 , 405 , 432 ] ]Line 1: We import the Pandas module as pd.
Line 2 to 4: We read the csv using the Pandas library read_csv and converted it into a dataframe (df). Then we convert each row into a list and assign the result to the list_of_csv variable. Now, in the next line, we are adding one list item at position 0 of the list_of_csv (list variable). This list item is the name of columns of the CSV file data.
Line 5: The output above shows that our CSV data is now successfully converted into the list and the first list value is the name of columns (header).
Conclusion
In this blog, we have learnt about how to convert the csv file data into a list. We have seen all different methods of list data structure like tuples, dictionaries. We have also seen the same method with the Pandas library. Then we have also seen how to add the header of the CSV into the list.