315-6 Returning Multiple Variables

In the snippet of code for the answer to #6:

  1. Can someone explain the positioning for the return of the “if header” portion? It looks like the data[0] is the first list element, which returns the header data and data[1:] is the remaining data, but the answer returns the body and then the header (in that order)? Is there a reason for this order?

  2. Because of question #1 return order, the header is now [1] and body [0] is opposite for all_data? And with that said, I actually don’t see the header data in the Output when I attempt to print(all_data), but of course I do see it when I print(header). Easy nuance, but perplexing at the moment.

Provided answer:

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

all_data = open_dataset()
header = all_data[1]
apps_data = all_data[0]
1 Like

I also got in trouble with that exercise.
At first my answer was:
all_data=open_dataset()
header=all_data[0]
apps_data=all_data[1:]

But it refused to accept the code.
I had to see the correct sollution and it showed me that apps_data=all_data[1]. I had no clue why, cause I thought that [1] is the row after the header

This post has a very good explanation for why all_data[1] was used in the solution.

Hi, @april.g. I got the point very quickly. The logic here can be explained this way:

if header:
return a, b
else:
return data

all_data=open_dataset()
header=all_data[a]
apps_data=all_data[b]

Instead of “a” and “b” we can place any values we want. It’s like in list, we just put the index we want

Here I made this way:
if header:
return data[1:],data[0]
else:
return data

all_data=open_dataset()
header=all_data[1]
apps_data=all_data[0]

But also can be like that if somebody likes:

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

all_data=open_dataset()
header=all_data[0]
apps_data=all_data[1]

Basically in return data[0],data[1:] , these elements are list that has index. Data[0] - has index 0; data[1:] has index 1. For me it’s all clear.

1 Like