Question: Instructions Write a function get _ list _ avg ( ) that expects a parameter main _ list ( a list of lists ) and

Instructions
Write a function get_list_avg() that expects a parameter main_list (a list of lists) and returns a new list that contains the average of each sub-list in main_list. The function should return an empty list if the main_list is empty.
The example below guides you step-by-step through the process. If you understand what needs to happen and how to accomplish it, then you can go directly to writing your function and testing it with the assert statements.
### Check that the computation is correct
assert get_list_avg([[10,0,2],[5,10,6],[7,8,9]])==[4.0,7.0,8.0]
assert get_list_avg([[10,0,2],[5,10,6],[7,8,9],[5,10,6]])==[4.0,7.0,8.0,7.0]
assert get_list_avg([[10,0,2,5,10,6],[7,8,9]])==[5.5,8.0]
### Verify that the original list is unchanged
sample_list =[[10,0,2],[5,10,6],[7,8,9]]
get_list_avg(sample_list) # call the function on a sample list
assert sample_list ==[[10,0,2],[5,10,6],[7,8,9]] # verify that sample list was unchanged
Walkthrough Example
Given a sample list [[10,0,2],[5,10,6],[7,8,9]], it can be formatted as follows to show its structure (3 nested sub-lists):
num_list =[
[
10,
0,
2
],
[
5,
10,
6
],
[
7,
8,
9
]
]
Loop over the main list to retrieve each element of this list, which is also a list:
for sublist in num_list:
print(sublist)
and produce
[10,0,2]
[5,10,6]
[7,8,9]
Compute an average of each sub-list, which is the sum of that list divided by the number of elements in that list.
To assemble a new list that would hold the average values of each sub-list, we need to create an empty list outside of the loop and then append the computed average values at the end of each iteration.
list_avg =[]
for sublist in num_list:
...
list_avg.append(avg)
print(list_avg)
Now that you have each step, turn them into a proper function that returns the correct result.
Hints
Refer to the following zyBook resources to review how to work with nested lists:
PA 7.5.1: List nesting.
PA 7.5.5: Iterating over multi-dimensional lists.
zyDE 7.3.2: Using built-in functions with lists.
zyDE 7.5.1: Two-dimensional list example: Driving distance between cities.
Figure 7.5.4: Iterating through multi-dimensional lists using enumerate().
Remember that you wrote calc_average() in the previous lab - use it as a helper function here.
 Instructions Write a function get_list_avg() that expects a parameter main_list (a

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!