Functions: intermediate 7/12

https://app.dataquest.io/m/316/functions%3A-intermediate/7/more-about-tuples

This was the assignment.

  1. Use the open_dataset() function to open the AppleStore.csv file, which has a header row.
  2. Do the variable assignment step in a single line of code.
  • Assign the header to a variable named header.
  • Assign the rest of the dataset to a variable named apps_data.
def open_dataset(file_name='AppleStore.csv', header=True):        
    opened_file = open(file_name)
    from csv import reader
    read_file = reader(opened_file)
    data = list(read_file)
    
    if header:
        return data[1:], data[0]
    else:
        return data

apps_data, header = open_dataset()

This is the solution code provided by DQ. I don’t understand how this works, when is apps_data being defined?

Can you please clarify what you mean by this?

Because you did share the code where it’s defined -

I guess, how does Python know apps_data refers to the whole dataset minus the header?

Your function is supposed to return two values.

One value is the header. The other is the dataset without the header.

return data[1:], data[0]

Then, when you call the function

apps_data, header = open_dataset()

You save the output of the function (those two values in the return statement) into the two variables, apps_data and header respectively.

Ah, I see, that’s super clear now! Thanks so much!

1 Like

why is the answer wrong when you invert the code:

header, apps_data = open_dataset()

Because when you change the variable names they will store different values than what the grader expects them to.