Shorthand for Reading CSV Files

When reading CSV files, I frequently abbreviate the process to the following format:

sample_file = list(reader(open(“sample.csv”)))

However, I noticed that the solutions provided by DQ usually break these out into individual steps on different lines. Is it okay to do it the way that I’ve been using? I’m assuming that there may be future situations when I do not necessarily want to convert the dataset to a list right away (maybe use a Pandas dataframe instead?), but in the Python Fundamentals stage, it seems like a quicker option. Just want to make sure that I’m not missing anything.

Hi @ammacdonald3

from csv import reader
opened_file = open(file_name)
read_file = reader(opened_file)
data = list(read_file)

and

data = list(reader(open(file_name)))

Both are doing the same thing.

Students will not get confused that’s why Dataquest writes it into breakable form.

1 Like