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)
Operations
In mathematics and computer science, an operation is an event that is carried out to satisfy a given task. Basic operations of a computer system are input, processing, output, storage, and control.
Basic Operators
An operator is a symbol that indicates an operation to be performed. We are familiar with operators in mathematics; operators used in computer programming are—in many ways—similar to mathematical operators.
Division Operator
We all learnt about division—and the division operator—in school. You probably know of both these symbols as representing division:
Modulus Operator
Modulus can be represented either as (mod or modulo) in computing operation. Modulus comes under arithmetic operations. Any number or variable which produces absolute value is modulus functionality. Magnitude of any function is totally changed by modulo operator as it changes even negative value to positive.
Operators
In the realm of programming, operators refer to the symbols that perform some function. They are tasked with instructing the compiler on the type of action that needs to be performed on the values passed as operands. Operators can be used in mathematical formulas and equations. In programming languages like Python, C, and Java, a variety of operators are defined.
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().](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F979d0aba-5428-414f-a3ba-5510f0301082%2F0c2e4e81-532e-4088-a745-f631360ea9ca%2Fkb452vb_processed.png&w=3840&q=75)
![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](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F979d0aba-5428-414f-a3ba-5510f0301082%2F0c2e4e81-532e-4088-a745-f631360ea9ca%2F4s2sbie_processed.png&w=3840&q=75)

Step by step
Solved in 3 steps with 1 images









