0

Fancy Houses' number of prices above average price...why is this not working?

prices = [125000, 78000, 110000, 65000, 300000, 250000, 210000, 150000, 165000, 140000, 125000, 85000, 90000, 128000, 230000, 225000, 100000, 300000] #your code goes here x=sum(prices) y=len(prices) avge=int(x/y) for n in prices: if n<avge: prices.remove(n) print(len(prices))

3rd Mar 2022, 3:00 PM
Cathyboum
6 Answers
+ 1
do we have to print the numbers which are greater than the average?? if yes then: You used ... n<avge ... // else please link the whole code description here.
3rd Mar 2022, 3:28 PM
NEZ
NEZ - avatar
+ 1
This is one of the common issues in python loops. You may try using list comprehension, lambda or count method to overcome this issue. See https://stackoverflow.com/questions/10665591/how-to-remove-list-elements-in-a-for-loop-in-python
3rd Mar 2022, 3:54 PM
Simba
Simba - avatar
+ 1
# why this is not working? # because the way how python handles iterations # take a look at this example it is similar to your problem x = ['a', 'b', 'c', 'd', 'e'] for i in x: print(i) x.remove(i) print(x) # output of the above code is # a # c # e # ['b', 'd'] # why ? # why python skipped those two elements and not removed them ? ''' 1st iteration : the value of i is 'a' the 0th index, we are printing it and then removing it on the next line so the new list will be a = ['b', 'c', 'd', 'e'] 2nd iteration : value of i will be 'c' because this is second iteration and iterator donno that we have removed the element from the list so the value of i will be 1th index which is 'c' so it will print it and will remove it .. so the new list will be ['b', 'd', 'e'] 3rd iteration : the value of i will be 'e' because on 3rd iteration python uses 2nd index value from list which is 'e' and will print it and will remove it
5th Mar 2022, 9:00 AM
Ratnapal Shende
Ratnapal Shende - avatar
+ 1
Note: indexing starts from 0th So this is why python skipps the elements and behaves unexpected! Hope you got it ! '''
5th Mar 2022, 9:00 AM
Ratnapal Shende
Ratnapal Shende - avatar
0
You can do this by creating 2 list compherasian The 1st list will loop over and add it all together Then second can just filter the price You then print second list
3rd Mar 2022, 5:02 PM
Ion Kare
Ion Kare - avatar
0
#Fancy Houses' number of prices above average price...why is this not working? thats how i solved itvvv prices = [125000, 78000, 110000, 65000, 300000, 250000, 210000, 150000, 165000, 140000, 125000, 85000, 90000, 128000, 230000, 225000, 100000, 300000] k = [i for i in prices] m = sum((k)) / len(k) p = [i for i in k if i > m] print(len(p))
3rd Mar 2022, 5:25 PM
Ion Kare
Ion Kare - avatar