38.2 Practice Help - Analyze to Realize | Sololearn: Learn to code for FREE!
+ 1

38.2 Practice Help - Analyze to Realize

Problem: You’re analyzing a data set and need to remove the outliers (the smallest and largest values. The data is stored in a list). Complete the code to remove the smallest and largest elements from the list and output the sum of the remaining numbers. I’m very lost. Having trouble from the lesson on how to pull the min and max and get rid of them let alone add it all up after. Code: data = [7, 5, 6.9, 1, 8, 42, 33, 128, 1024, 2, 8, 11, 0.4, 1024, 66, 809, 11, 8.9, 1.1, 3.42, 9, 100, 444, 78] #your code goes here data.remove(max(data)): data.remove(min(data)): print(data) thanks!

2nd Apr 2021, 1:55 AM
Zach Z
15 ответов
+ 6
At the Code part, after "#your code goes here", the lesson shows us how to get rid of the max and min values of the list: data.remove(max(data)) data.remove(min(data)) The focus of the lesson remain in output the sum of the rest of the list. And for this we can use the sum() function, which takes an iterable element as argument (i.e. our list) and return the sum of it's elements. To show the result, you can do: print(sum(data)) This should work. 👍
2nd Apr 2021, 2:15 AM
thequietprogrammer
thequietprogrammer - avatar
+ 1
Hey Zach Z just do data.remove (max(data)) data.remove (min(data)) print(sum(data))
2nd Apr 2021, 12:22 PM
Mohd Aadil
Mohd Aadil - avatar
+ 1
Can I use „for” loop instead of sum() functon? data = [7, 5, 6.9, 1, 8, 42, 33, 128, 1024, 2, 8, 11, 0.4, 1024, 66, 809, 11, 8.9, 1.1, 3.42, 9, 100, 444, 78] #your code goes here data.remove(max(data)) data.remove(min(data)) sum = 0 for i in data: sum += i print(sum) Result is the same.
29th Apr 2021, 4:05 PM
Marek Marczak
Marek Marczak - avatar
+ 1
There is still an issue with the exercice: 1024 is twice in the list. Data.remove only deletes one of the values from the list.
21st May 2021, 1:47 PM
Ptr59
Ptr59 - avatar
+ 1
The answer is as simple as this: data.remove(min(data)) data.remove(max(data)) print(sum(data))
29th Sep 2021, 1:13 AM
Nourou Dine Cisse
Nourou Dine Cisse - avatar
+ 1
by this function we can remove all min and max (we have 3 112 and 3 1) l=[1,1,1,1,2,3,4,4,4] MAX = max(l) O=l.count(MAX) MIN= min(l) P=l.count(MIN) while O!=0 or P!=0 : if MAX in l: l.remove(MAX) O=O-1 if MIN in l: l.remove(MIN) P=P-1 print(l)
2nd Oct 2021, 11:07 AM
Mahdi Bitaab
Mahdi Bitaab - avatar
+ 1
data = [7, 5, 6.9, 1, 8, 42, 33, 128, 1024, 2, 8, 11, 0.4, 1024, 66, 809, 11, 8.9, 1.1, 3.42, 9, 100, 444, 78] #your code goes here data.remove(min(data)) data.remove(max(data)) sum = 0 for nums in range(0, len(data)): sum = sum + data[nums] print(sum)
16th May 2022, 8:34 AM
Junior Jackson
Junior Jackson - avatar
+ 1
data = [7, 5, 6.9, 1, 8, 42, 33, 128, 2, 8, 11, 0.4, 1024, 66, 809, 11, 8.9, 1.1, 3.42, 9, 100, 444, 78] lv=max(data) sv=min(data) data.remove(lv) data.remove(sv) print(sum(data))
2nd Mar 2023, 2:36 PM
Fathima H.B
0
It stated that output the sum of the remaining numbers. You printed the whole data.
2nd Apr 2021, 2:14 AM
你知道規則,我也是
你知道規則,我也是 - avatar
0
hi there, I also just tried to solve this exercise and was wondering why my first solution using a for loop does not work: data = [7, 5, 6.9, 1, 8, 42, 33, 128, 1024, 2, 8, 11, 0.4, 1024, 66, 809, 11, 8.9, 1.1, 3.42, 9, 100, 444, 78] #your code goes here min=min(data) max=max(data) sum=0 for num in data : if num==min or num==max: data.remove(num) else: sum+=num print(sum) Above solution works fine if I try it in a Python console, but surprisingly gives me a strange float as sum output. However, this in my opinion takes care of the max being twice in the data list. The below solution does work in SoloLearn, but it does not actually make sense to me why. Firstly, it does not remove items in the list that occur more than once and secondly, it does not work in the Python console. The following code data.remove(min(data)) data.remove(max(data)) print(sum(data)) executed in Python console gives me: sum(data) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'float' object is not callable Anyone have more insight into this? Thanks!
10th Jun 2021, 10:45 AM
Judith Levy
Judith Levy - avatar
0
Is this the entire question or there was an adittional note at the end.
12th Jun 2021, 12:23 AM
Nana King Obedson
Nana King Obedson - avatar
0
The note at the end is: Remember the functions a list supports: min and max can be used to get the smallest and largest numbers. But I think it is not relevant regarding this thread how to solve the exercise
12th Jun 2021, 5:10 PM
Judith Levy
Judith Levy - avatar
0
data = [7, 5, 6.9, 1, 8, 42, 33, 128, 1024, 2, 8, 11, 0.4, 1024, 66, 809, 11, 8.9, 1.1, 3.42, 9, 100, 444, 78] print(len(data)) print(max(data)) print(min(data)) data.remove(max(data)) data.remove(min(data)) print(data) print("the sum of the data is \n") print(sum(data))
26th Jul 2021, 2:16 PM
Sahukari Krishna Santosh
Sahukari Krishna Santosh - avatar
0
Thanks for the hint) Again I am convinced that you need to be careful when reading the assignment. But I have a question: 1024 occurs twice, and we remove only one 1024 (command to print the list). Isn't it a mistake to delete only one 1024? If so, what is the correct way to solve this problem? Thanks.
30th Jul 2021, 9:57 AM
Tsimur Kuts
Tsimur Kuts - avatar
- 1
Thanks! thequietprogrammer had : for some reason. Missing Sum() too.
4th Apr 2021, 9:35 PM
Zach Z