Question regarding reusing functions inside another function's definition

Screen Link: https://app.dataquest.io/m/315/functions%3A-fundamentals/11/combining-functions

My Code: opened_file = open(‘AppleStore.csv’)
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)

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

def find_sum(a_list):
a_sum = 0
for element in a_list:
a_sum += float(element)
return a_sum

def find_length(a_list):
length = 0
for element in a_list:
length += 1
return length

def mean(data_set, index):
column = extract(data_set, index)
return find_sum(column) / find_length(column)

avg_price = mean(apps_data, 4)

I do not understand why we need to use: column = extract(data_set, index) and can not just use return find_sum(a_list) / find_length(a_list) ?

I am asking myself what is ‘a_list’ doing in the sum and length functions before, when I do not define it and also do not use it?

Thank you all!

Hi @tillvomheede,

We cannot use directly return find_sum(a_list) / find_length(a_list), because, according to the task, we don’t have any list yet. Instead, we have a dataset and an index of a column (according to the first 2 lines of the task), for which we want to calculate the mean. Using the extract() function inside the mean(), we get the values of this column as a list called column, for further using it to find the mean.

In the find_sum() and find_length() functions, we take any list as an input (here it’s called a_list, but of course it can be called whatever). And we try to reconstruct the behaviour of the already existing functions sum() and length(), just to get some practice of writing functions.

1 Like