{"id":5285,"date":"2021-05-10T09:32:08","date_gmt":"2021-05-10T04:02:08","guid":{"rendered":"https:\/\/python-programs.com\/?p=5285"},"modified":"2021-11-22T18:42:49","modified_gmt":"2021-11-22T13:12:49","slug":"python-read-csv-into-a-list-of-lists-or-tuples-or-dictionaries-import-csv-to-list","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-read-csv-into-a-list-of-lists-or-tuples-or-dictionaries-import-csv-to-list\/","title":{"rendered":"Python: Read CSV into a list of lists or tuples or dictionaries | Import csv to list"},"content":{"rendered":"
In this article, we will demonstrate how we can import a CSV into a list, list of lists or a list of tuples in python. We will be using pandas module for importing CSV contents to the list without headers.<\/p>\n
Example Dataset :<\/p>\n
CSV File name – data.csv<\/p>\n
Id,Name,Course,City,Session\r\n21,Jill,DSA,Texas,Night\r\n22,Rachel,DSA,Tokyo,Day\r\n23,Kirti,ML,Paris,Day\r\n32,Veena,DSA,New York,Night<\/pre>\nRead a CSV into list of lists in python :<\/h3>\n
1. Importing csv to a list of lists using csv.reader :<\/strong><\/p>\n
CSV.reader<\/code> is a python built-in function from the CSV module which will help us read the CSV file into the python. Then passing the reader object into the
list()<\/code> will return a list of lists.<\/p>\n
Let’s see the implementation of it.<\/p>\n
#Program :\r\n\r\nfrom csv import reader\r\n\r\n#Opening the csv file as a list of lists in read mode\r\nwith open('data.csv', 'r') as csvObj:\r\n #The object having the file is passed into the reader\r\n csv_reader = reader(csvObj)\r\n #The reader object is passed into the list( ) to generate a list of lists\r\n rowList = list(csv_reader)\r\n print(rowList)\r\n<\/pre>\nOutput :\r\n[['Id', 'Name', 'Course', 'City', 'Session'], \r\n['21', 'Jill', 'DSA', 'Texas', 'Night'], \r\n['22', 'Rachel', 'DSA', 'Tokyo', 'Day'], \r\n['23', 'Kirti', 'ML', 'Paris', 'Day'], \r\n['32', 'Veena', 'DSA', 'New York', 'Night']]<\/pre>\n2. Selecting specific value in csv by specific row and column number :<\/strong><\/h4>\n
\u00a0<\/strong>We can also select particular rows and columns from the CSV file by using Pandas. We have to read the CSV into a dataframe excluding the header and create a list of lists.<\/p>\n
Let’s see the implementation of it.<\/p>\n
#Program :\r\n\r\nimport pandas as pd\r\n\r\n# Create a dataframe from the csv file\r\ndfObj = pd.read_csv('data.csv', delimiter=',')\r\n# User list comprehension \r\n# for creating a list of lists from Dataframe rows\r\nrowList = [list(row) for row in dfObj.values]\r\n# Print the list of lists i.e. only rows without the header\r\nprint(rowList)\r\n<\/pre>\nOutput :\r\n[[21, 'Jill', 'DSA', 'Texas', 'Night'], \r\n[22, 'Rachel', 'DSA', 'Tokyo', 'Day'], \r\n[23, 'Kirti', 'ML', 'Paris', 'Day'], \r\n[32, 'Veena', 'DSA', 'New York', 'Night']]<\/pre>\n3. Using Pandas to read csv into a list of lists with header :<\/h4>\n
To include the header row, we can first read the other rows like the previous example and then add the header to the list.<\/p>\n
Let’s see the implementation of it.<\/p>\n
#Program :\r\n\r\nimport pandas as pd\r\n\r\n# Create a dataframe from the csv file\r\ndfObj = pd.read_csv('data.csv', delimiter=',')\r\n# User list comprehension \r\n# for creating a list of lists from Dataframe rows\r\nrowList = [list(row) for row in dfObj.values]\r\n#Adding the header\r\nrowList.insert(0, dfObj.columns.to_list())\r\n# Print the list of lists with the header\r\nprint(rowList)\r\n<\/pre>\nOutput :\r\n[['Id', 'Name', 'Course', 'City', 'Session'], \r\n[21, 'Jill', 'DSA', 'Texas', 'Night'], \r\n[22, 'Rachel', 'DSA', 'Tokyo', 'Day'], \r\n[23, 'Kirti', 'ML', 'Paris', 'Day'], \r\n[32, 'Veena', 'DSA', 'New York', 'Night']]<\/pre>\nReading csv into list of tuples using Python :<\/h3>\n
Let\u2019s add the contents of CSV file as a list of tuples. Each tuple will be representing a row and each value in the tuple represents a column value. Just like the way we added the contents into a list of lists from CSV, we will read the CSV file and then pass it into list function to create a list of tuples. The only difference here is the
map( )<\/code> function that accepts function and input list arguments.<\/p>\n
Let’s see the implementation of it.<\/p>\n
#Program :\r\n\r\nfrom csv import reader\r\n# open file in read mode\r\nwith open('data.csv', 'r') as readerObj:\r\n # here passing the file object to reader() to get the reader object\r\n csv_reader = reader(readerObj)\r\n #Read all CSV files into the tuples\r\n tuplesList = list(map(tuple, csv_reader))\r\n # display the list of tuples\r\n print(tuplesList)\r\n<\/pre>\nOutput :\r\n\r\n[('Id', 'Name', 'Course', 'City', 'Session'), ('21', 'Jill', 'DSA', 'Texas', 'Night'), ('22', 'Rachel', 'DSA', 'Tokyo', 'Day'), ('23', 'Kirti', 'ML', 'Paris', 'Day'), ('32', 'Veena', 'DSA', 'New York', 'Night')]<\/pre>\nReading csv into list of tuples using pandas & list comprehension :<\/h3>\n
We can load the contents of a CSV file into a dataframe by using
read_csv( )<\/code> . Then using list comprehension we can convert the 2D numpy array into a list of tuples.<\/p>\n
Let’s see the implementation of it.<\/p>\n
#Program :\r\n\r\nimport pandas as pd\r\n# Create a dataframe object from the csv file\r\ndfObj = pd.read_csv('data.csv', delimiter=',')\r\n# Create a list of tuples for Dataframe rows using list comprehension\r\ntuplesList = [tuple(row) for row in dfObj.values]\r\n# Print the list of tuple\r\nprint(tuplesList)\r\n<\/pre>\nOutput :\r\n[(21, 'Jill', 'DSA', 'Texas', 'Night'), (22, 'Rachel', 'DSA', 'Tokyo', 'Day'), (23, 'Kirti', 'ML', 'Paris', 'Day'), (32, 'Veena', 'DSA', 'New York', 'Night')]<\/pre>\nReading csv into list of dictionaries using python :<\/h3>\n
We can also read the contents of a CSV file into dictionaries in python where each dictionary in the list will be a row from the CSV file. The CSV file contents are opened in read mode then they are passed into the
Dict_reader( )<\/code> as a reader object, then it is passed into the list.<\/p>\n
Let’s see the implementation of it.<\/p>\n
#Program :\r\n\r\nfrom csv import DictReader\r\n# open file in read mode\r\nwith open('data.csv', 'r') as readerObj:\r\n # pass the reader file object to DictReader() to get the DictReader object\r\n dict_reader = DictReader(readerObj)\r\n # get a list of dictionaries from dct_reader\r\n dictList = list(dict_reader)\r\n # print the list of dict\r\n print(dictList)\r\n<\/pre>\nOutput :\r\n\r\n[OrderedDict([('Id', '21'), ('Name', 'Jill'), ('Course', 'DSA'), ('City', 'Texas'), ('Session', 'Night')]), OrderedDict([('Id', '22'), ('Name', 'Rachel'), ('Course', 'DSA'), ('City', 'Tokyo'), ('Session', 'Day')]), OrderedDict([('Id', '23'), ('Name', 'Kirti'), ('Course', 'ML'), ('City', 'Paris'), ('Session', 'Day')]), OrderedDict([('Id', '32'), ('Name', 'Veena'), ('Course', 'DSA'), ('City', 'New York'), ('Session', 'Night')])]<\/pre>\n","protected":false},"excerpt":{"rendered":"Read CSV into a list of lists or tuples or dictionaries | Import csv to list in Python. In this article, we will demonstrate how we can import a CSV into a list, list of lists or a list of tuples in python. We will be using pandas module for importing CSV contents to the …<\/p>\n