Variables order in solution

Hello, why is order of variables in solution this way, apps_data, header = open_dataset() ?
If i switch them, i get wrong results if i click to variables inspector.
So if i use solution this way header, apps_data = open_dataset() i get all data for header, and header data for apps_data variable.
I can see that it depends on position in condition if header: return data[1:], data[0] because when i switch them, it works opposite in solution.

So it is confusing for me, as in previous step 316-6 it was said, that return data[1:] is meant for header and data[0] for rest of data, so i would expect that function will always return just header data first, but here it works opposite for some reason.

I can explain it myself only in way, that in solution function return on first position variable, which is closest to the “=” mark and in that case it is really header which is in the closest position in solution apps_data, header = open_dataset() so it returns like open_dataset () = header, apps_data in that order even if the solution is written seemingly opposite way, starting with apps_data variable.

Maybe i answered myself to my question, but just to be sure, can someone please clarify and confirm? Many thanks!

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]
    return data

apps_data, header = open_dataset()

Click here to open the screen in a new tab.

Hi @eelke78,

Yes, the order is important here. The order of the variables is always the same as the order of the return statement in your function. So if you function has this return statement

if header:
        return data[1:], data[0]

it will always return first the table itself, then the header, even though you can call your variables whatever you want.

Hi Elena, thank you for your reply.

Does “return data[1:]” return header first, if i read from left to right? I think so, that way it worked in previous step 316-6. If yes, then it puzzled me that in solution we defined first apps_data variable from left, then header. But apps_data variable is actually second position of open_dataset function, right?


No, return data[1:], data[0] returns the body of the table first (data[1:]), then the header (data[0]).

It was so also in the step 316-6, look:

    if header:
        return data[1:], data[0]
        return data
all_data = open_dataset()
header = all_data[1]
apps_data = all_data[0]

In this case, all_data will be a tuple of this kind: (table body, header). Then the variable header will extract separately only the second element of it (that is, header), while apps_data - only the first element (i.e., table body)

1 Like

Cool, i think i get it! So at the end i was confused more of previous step 316-6 where we called for header variable all_data[1] but it actually called data[0] from return statement of open_dataset() function where it sits on 2nd position.

It also confirms what i learned so far, that 1st position in list/tuple has index 0.

Many thanks Elena!

1 Like

That’s great Karel, I’m glad it was helpful :blush: