+ 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!
15 Answers
+ 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. 👍
+ 1
Hey Zach Z
just do
data.remove (max(data))
data.remove (min(data))
print(sum(data))
+ 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.
+ 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.
+ 1
The answer is as simple as this:
data.remove(min(data))
data.remove(max(data))
print(sum(data))
+ 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)
+ 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)
+ 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))
0
It stated that output the sum of the remaining numbers.
You printed the whole data.
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!
0
Is this the entire question or there was an adittional note at the end.
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
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))
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.
- 1
Thanks! thequietprogrammer had : for some reason. Missing Sum() too.