Functions: Extract Values from a column screen 6/14 HELP

Hi! Please help me explain this code:

This is the instructions in my screen:

  1. Write a function named extract() that can extract any column you want from the apps_data dataset.
  • The function should take in the index number of a column as input (name the parameter however you want)
  1. Inside the function’s definition, do the following:
  • Create an empty list
  • Loop through the apps_data data set (excluding the header). Extract only the value you want by using the parameter (which will be an index number)
  • Append that value to the empty list
  • Return the list containing the values of the column

This is the code:

def extract(index):
    column =  [   ]
    for row in apps_data[1:]:
        value = row[index]
        column.append(value)    
    return column

Why do I need to have value= row[index], what does this mean?

Hi !

The extract function takes in index as an int argument, which represent a column of the apps_data set.

The row in the for loop, is one full row of the whole apps_data (minus the header).
To select only the correct element in the index column you need to get the (index+1)-th element of the list, (if index = 3, it would be the 4th element of row),
Hence row[index].

It’s best if you try out the function to see how it works.

Hope this help.

Hi @pauleenramos. Just FYI, I edited your post so that the code you provided is enclosed in a set of triple backticks (```) so that it is rendered in a more readable format, particularly with respect to the whitespaces/indentation that make it much more readable. Without the triple backticks, the extra spaces for for loops and if statements are automatically removed and it becomes difficult to see your code the way you intended it to be shown. You may want to check out this post for more great advice on asking technical questions in the community.

I think @aravinthbalakrishnan did a great job of explaining your question and I only want to add to it by specifically addressing your final question:

Technically speaking, we don’t need it! We could have easily avoided assigning row[index] to a variable (ie value) and just done the following:

def extract(index):
    column =  [   ]
    for row in apps_data[1:]:
        column.append(row[index])    
    return column

Since the purpose of this function is to return a particular column, the really important variable we need to build is column. However, in my opinion, assigning row[index] to the variable value makes it a little easier to read and understand. But strictly speaking, it is not needed and can be avoided if you wanted to, as is shown above.