- 2

Python for beginners: Analyze To Realize (List Functions)

Hi Guys, I'm trying to figure out the correct solution for this problem. The code seems to work fine in an IDE, but I can't get a pass on SoloLearn. :/ The task is to complete the code to remove the smallest and largest elements from the list and output the sum of the remaining numbers. # The given list: 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] # My code: max = max(data) min = min(data) i_max = data.count(max) i_min = data.count(min) for i in range(i_max): data.remove(max) for i in range(i_min): data.remove(min) total = 0 for i in data: total += i print(total)

16th Mar 2021, 2:08 PM
imMike
imMike - avatar
4 Réponses
+ 1
however, it is bad practice to use built-in names for your variables: in your code, you cannot access more than once the min and max built-in functions as you are overiding their value with the result ;P
16th Mar 2021, 3:06 PM
visph
visph - avatar
0
just remove min and max from data (without count, loop...) then compute sum of remaining elements and output it ^^ max = max(data) min = min(data) #i_max = data.count(max) #i_min = data.count(min) #for i in range(i_max): # data.remove(max) #for i in range(i_min): # data.remove(min) data.remove(max) data.remove(min) total = 0 for i in data: total += i print(total)
16th Mar 2021, 3:04 PM
visph
visph - 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] #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
- 3
Thank you @visph! I tried that before, but I must have made some silly mistake. :) Take care!
16th Mar 2021, 3:18 PM
imMike
imMike - avatar