list.remove(item) question (Python3) | Sololearn: Learn to code for FREE!

+1

list.remove(item) question (Python3)

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] y=min(data) z=max(data) for x in data: if x==y in data: data.remove(y) elif x==z in data: data.remove(z) print(data) total+=x print(total) I under stand that the x is indexing the data list every loop and my code should be checking to see if and elif are true and then removing the min or max but for some reason it's not removing the second 1024. Can anyone explain this? Thanks

5/26/2021 11:32:58 PM

Matt

15 Answers

New Answer

+3

Matt If you are using loop then doens't make sense to remove item from data then add x. You can simply do this: for x in data: if x != y and x != z: total += x -------------------------------+++---------- Btw Only one item will be remove. You just have to remove min and max value then calculate addition Or you can first add all then remove min and max value. So you can do in 2 ways: y = min(data) z = max(data) 1 - print(sum(data) - y - z) 2 - data.remove(y) data.remove(z) print (sum(data)) https://code.sololearn.com/cjLEHtmC9cGn/?ref=app

+3

Christopher Comstock ( ͡° ͜ʖ ͡°) (╹◡╹) No problem just keep doing practice. You will know everything.

+3

Christopher Comstock ( ͡° ͜ʖ ͡°) (╹◡╹) Why I will do. I just encourage you.

+3

Christopher Comstock ( ͡° ͜ʖ ͡°) (╹◡╹) I just do downvote on nonsense things.

+3

Miqayel G How do you know? Do you have proof? Now I will downvote your comment because you are blaming me without any proof and also for that posting unnecessary comments in threads.

+2

I think there are many errors in your code First you need to define total variable before the loop Second you don't need to use (in) when you are checking for equality And about your question(why 1024 didn't get removed) That because list are mutable So when for loop starts and removes the first 1024, the max of data changes, so max(data) is no longer 1024

+2

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] y=0.4 z=1024 total=0 while y in data: for x in data: if x==y: data.remove(x) total+=x while z in data: for x in data: if x==z: data.remove(x) total+=x print(total) print(data)

+2

Why i am the only one who don’t know anything about python! 😪😖😣☹️

+2

Did you downote my comment? 🅰🅹 🅐🅝🅐🅝🅣

+2

Christopher Comstock ( ͡° ͜ʖ ͡°) (╹◡╹) Try it. It's interesting. I'm not a programmer and i don't know a thing about programming, i learn python just for fun

+2

Christopher Comstock ( ͡° ͜ʖ ͡°) (╹◡╹) Keep it up Python has a huge community and fans

+1

ᎪᏞᎪ'Ꭺ ᎪᏞᎠᎬᎬΝ ՏᎻᎪᎷᎷᎡ you mean i should learn Python? I learned Python for Beginners. And i did 14 code couch solutions or something like this

+1

It's okay guys, I'll upvote all of your comments. Just let it go

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] y=min(data) z=max(data) total=0 data1=[x for x in data if x !=y if x!=z] print(data1) total=sum(data1) print(total)