In this article we will discuss how to import a CSV into list. This list can be a list of lists, list of tuples or list of dictionaries. We will also use pandas module and cover scenarios for importing CSV contents to list with or without headers.

Suppose we have a CSV file ‘students.csv’ and its contents are,

Now let’s see how to import the contents of this csv file into a list.

Read a CSV into list of lists in python

There are different ways to load csv contents to a list of lists,

Import csv to a list of lists using csv.reader

Python has a built-in csv module, which provides a reader class to read the contents of a csv file. Let’s use that,

Output:

It created a list of lists containing all rows of csv file and print that list of lists.

How did it work ?

We opened the file in read mode and then passed the file object to csv.reader() function. It returned an iterator, which can be used to iterate over all the lines of csv file. But we passed this iterator object to list() function, which return a list of lists i.e. where each list represents a row of csv and each item in the list represents a cell / column in that row.

Select specific value in csv by specific row and column number

We can also select an individual element in csv file by row & column number using this list of lists created above. For example, from the list of lists i.e. list_of_rows (created above), let’s select the value from csv at row number 3 and column number 2,

Output:

Use Pandas to read csv into a list of lists without header

In the previous example, we loaded all rows (including header) into a list of lists. Suppose we want to read all rows into a list of lists except header. We can achieve that easily using pandas,

Output:

It created a list of lists containing all rows of csv except header and print that list of lists.

How did it work ?

We loaded the csv to a Dataframe using read_csv() function. As Dataframe.values returns a 2D Numpy representation of all rows of Dataframe excluding header. So, we iterated over all rows of this 2D Numpy Array using list comprehension and created a list of lists. Where each list represents a row of csv and each item in the list represents a cell / column in that row.

Use Pandas to read csv into a list of lists with header

In above example, header of csv was skipped by default. So, if you want header too in this list of lists,
then we need to insert it in list separately in the end of the above example, like this,

Output:

It created a list of lists containing all rows of csv including header and print that list of lists.

How did it work?

We loaded the csv to a Dataframe using read_csv() function. As Dataframe.values returns a 2D numpy representation of all rows of Dataframe excluding header. So, we iterated over all rows of this 2D Numpy Array using list comprehension and created a list of lists. Dataframe.columns gives column names, so we converted that to a list and inserted at the start of the rows list.

Read csv into list of tuples using Python

Let’s load all the contents of students.csv to a list of tuples, where each tuple in the list represents a row and each value in the tuple represents a cell / column value for that particular row.

Output

How did it work?

We opened the csv file in read mode and then passed the file object to csv.reader() function.It returned an iterator csv_reader, with which we can iterate over all the rows of csv. But we passed it into the map() function as an argument along with tuple() function as callback i.e.,

map() function accepts a function & input list as arguments. For each item in the input list it applies the function and stores the result in a mapped object.

So, in this case map() function, iterated over all the rows of csv using iterator csv_reader and applied the function tuple() to each item. Then stored the returned tuple i.e. a tuple for a row, to the mapped object. So, now our mapped_object contains tuples, i.e. one tuple for each row.

Then we converted this mapped object of tuples to a list of tuples i.e.

So, this is how we imported a csv file to a list of tuples. We can do the same using pandas & list comprehension in a single line i.e.

Read csv into list of tuples using pandas & list comprehension

Output

We loaded the csv to a Dataframe using read_csv() function. Dataframe.values returned a Numpy representation of the DataFrame i.e. is a 2D numpy array. Using list comprehension we iterated over each row of this 2D array and for each row we converted that to a tuple, then append it to a list.
Finally list comprehension returned a list of tuples, where each tuple in the list represents a row and each value in the tuple represents a cell / column value for that particular row.

Read csv into list of dictionaries using python

Output:

We got a list of dictionaries, where each dictionary in the list represents a csv row and contains pairs of column names & column values for that row, as key / value pairs.

How did it work?

We opened the csv file in read mode and then passed the file object to csv.DictReader() function. It returned an iterator dict_reader, with which we can iterate over all the rows of csv and fetch each row content as a dictionary. But we passed this iterator to the list() function, which returned a list of dictionaries i.e. rows.

An important point is, whenever we pass an iterable item to a list constructor i.e. list(), then it internally iterates over all the elements of the data structure to which this iterator object is pointing
& stores them into a list. In the end returns that list.

The complete example is as follows,

Output:

Python Recommendations:

C++ & C++11 Recommendations:

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.

Subscribe with us to join 1500+ Python & C++ developers, to get more Tips &  Tutorials like this.