6) Returning multiple variables(code confusion)

Shouldn’t it be header = all_data[0] and apps_data = all_data[1:] instead of what it’s in the answer. Also confused with printed rows as mentioned in latter part of code. Please explain both in simplest way, Thank you for your help.

Screen Link:
https://app.dataquest.io/m/316/functions%3A-intermediate/6/returning-multiple-variables

My Code:

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
    
all_data = open_dataset()
header = all_data[1]
apps_data = all_data[0]
print(all_data[:5])
print(header)
print(apps_data[:5])

What I expected to happen: as i did [:5] expected only five rows to print but it printed all.

What actually happened:


2 Likes

Here in the function return statement we are returning first all data (data[1:]) and then second header (data[0]) so in tuple first item would be data and second item would be headers hence we are doing

Now

Here, all_data is tuple returned by function. That has two item first is all data and second is header. So if you do print(all_data[:5]) it will give you all data with header. To confirm it’s tuple you can note () brackets around print result.

Here,

by removing print(all_data[:5]) will give you expected output

print(header)
print(apps_data[:5])
2 Likes

what to do if want five rows only ? because i want to see what the output says for understanding better but cant check 7197 rows. so confused

Please referesh page i have updated my answer. You have to use apps_data not all_data like

print(apps_data[:5])
1 Like

great i will try that and revert back to you, thank you again for you quick response :slight_smile:

1 Like

Sir, can you share some more light to this logic, this is not explained in the theory part, in the theory part it just says that if we return sum first then we get sum first in the output. How can i understand this logic from that? Hope you understand.

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[0], data[1:]
else:
    return data

all_data = open_dataset()
header = all_data[0]
apps_data = all_data[1]
print(header)
print(apps_data[:5])

ok this is my code now @DishinGoyani, now its right? Not getting () when printing output of this code. Shouldn’t it be apps_data = all_data[1:] in the last third line? Need ur help in this, still struggling

Hi @uditchauhan,

I have explained this code here:

Hope it helps :slightly_smiling_face:

Best,
Sahil

2 Likes

Yes, I just tried your code and it is giving me expected output. so aren’t you getting same output?

But we only return two items from function in tuple so it doesn’t matter if you use all_data[1] or all_data[1:] both will give you same output.

In addition refer 6) Returning multiple variables(code confusion) @Sahil he has explained. I think that would clear your doubt.

1 Like

yes i am getting output, but i was confused at this

i thought i have to do this here also, thats why i asked again for your conformation, tbh the answer is confusing, the last code which i wrote is straightforward i think.

yes @Sahil sir, it will be best if i return the header first, because in return data[1:], data[0] we have to do this

in the end, which contradicts our default thinking that header is 0
so better i do this

so in the end when assigning comes we can do this

which is better in understanding as we dont have to shuffle for the tuples, and basic understanding that header is 0, any closure on this @Sahil @DishinGoyani ?

1 Like

Indeed that is better to avoid confusion. :slightly_smiling_face:

2 Likes

thanks a ton for the quick reply @DishinGoyani sir and for your guidance @Sahil sir. Back on track :slight_smile:

1 Like