that my answer: got the output: [4.0, 7.0, 8.0] but the system say: We got a return value None instead of [4.0, 7.0, 8.0] ----------------------------------------------------------------       def get_list_avg(main_list):     new_list = []     for i in range(len(main_list)):         sum1=0         for j in range(len(main_list)):             sum1 = sum1+main_list[i][j]         avg=(sum1/len(main_list))         new_list.append(avg)     print(new_list)      main_list=[[10,0,2],[5,10,6],[7,8,9]] get_list_avg(main_list)

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
icon
Concept explainers
Question
100%

that my answer:

got the output:

[4.0, 7.0, 8.0]

but the system say:

We got a return value

None

instead of [4.0, 7.0, 8.0]

----------------------------------------------------------------

 

 

 

def get_list_avg(main_list):
    new_list = []
    for i in range(len(main_list)):
        sum1=0
        for j in range(len(main_list)):
            sum1 = sum1+main_list[i][j]
        avg=(sum1/len(main_list))
        new_list.append(avg)
    print(new_list)
    
main_list=[[10,0,2],[5,10,6],[7,8,9]]
get_list_avg(main_list)

1
],
[
0
7,
8,
9
Loop over the main list to retrieve each element of this list, which is also a list:
and produce
for sublist in num_list:
print (sublist)
[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:
for sublist in num_list:
print (sublist)
avg sum(...) /
print (f"List average
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)
{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 5.5.1: List nesting.
•
PA 5.5.5: Iterating over multi-dimensional lists.
• zyDE 5.3.2: Using built-in functions with lists.
O
zyDE 5.5.1: Two-dimensional list example: Driving distance between cities.
• Figure 5.5.4: Iterating through multi-dimensional lists using enumerate().
Transcribed Image Text:1 ], [ 0 7, 8, 9 Loop over the main list to retrieve each element of this list, which is also a list: and produce for sublist in num_list: print (sublist) [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: for sublist in num_list: print (sublist) avg sum(...) / print (f"List average 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) {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 5.5.1: List nesting. • PA 5.5.5: Iterating over multi-dimensional lists. • zyDE 5.3.2: Using built-in functions with lists. O zyDE 5.5.1: Two-dimensional list example: Driving distance between cities. • Figure 5.5.4: Iterating through multi-dimensional lists using enumerate().
creating a new list and appending computation results (accumulator pattern for a list)
• using a for loop to iterate over lists inside a list (processing nested lists)
• assembling the provided instructions into a function that returns a new list
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, 21, [5, 10, 6], [7, 8, 911) == [4.0, 7.0, 8.01
assert get_list_avg ([[10, 0, 21, [5, 10, 6], [7, 8, 9], [5, 10, 6]]) == [4.0, 7.0, 8.0, 7.01
assert get_list_avg([[10, 0, 2, 5, 10, 61, [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, 21, [5, 10, 61, [7, 8, 911 # 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
Transcribed Image Text:creating a new list and appending computation results (accumulator pattern for a list) • using a for loop to iterate over lists inside a list (processing nested lists) • assembling the provided instructions into a function that returns a new list 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, 21, [5, 10, 6], [7, 8, 911) == [4.0, 7.0, 8.01 assert get_list_avg ([[10, 0, 21, [5, 10, 6], [7, 8, 9], [5, 10, 6]]) == [4.0, 7.0, 8.0, 7.01 assert get_list_avg([[10, 0, 2, 5, 10, 61, [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, 21, [5, 10, 61, [7, 8, 911 # 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
Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Operators
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education