Could someone please help me understand how to create an "extract" function in the course "Functions: Fundmentals"?

These are the instructions:

The answers are the following:
image

However, I don’t understand many aspects of the answer. I’ve written some comments, including my doubts. Could you guys kindly take a look at them and help me understand? Thank you!

Hi @taojian2009:

Do you mind formatting and pasting your code here as per these guidelines? It will be much easier to do so if you do that. Thanks!

def extract(index):

Here index is an argument passed into the function extract(), kind of how you need need inputs for a calculator to compute an output. index is the column number as stated on the mission instructions.

value = row[index]

Here we want to extract/retrieve the value stored in the particular column. The dataset is a list of lists so at each iteration of the for loop, we are retrieving the value stored in the row at which the for loop is currently on and the corresponding value in the specified column number (i.e. index). Appending this value to an empty dictionary essentially retrieves all values in that particular column.

return column

Return does not execute a function, it gives back variable(s) in this case the list or the values stored in the column variable back to the main function (outside of extract()). Only calling the function by invoking extract(index) will make the function execute (without the def keyword). You might want to read this article.

Also something to note as mentioned in the article

Well, one possibility is to use function return values. A return statement in a Python function serves two purposes:

1. It immediately terminates the function and passes execution control back to the caller.
2. It provides a mechanism by which the function can pass data back to the caller.