Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMason Lim
1,209 PointsHow do i use del function on list of integers?
"Oh no! I have another messy list. This one has some even numbers in it and I want it to only have odd numbers. Use del to delete the 8 from messy." Currently stuck on this challenge. I can't seem to use index for numbers to delete the 8.
messy = [5, 2, 8, 1, 3]
del messy(2)
4 Answers
Chris Freeman
Treehouse Moderator 68,454 PointsYou're very close. The syntax for a list index is square brackets[ ]
messy = [5, 2, 8, 1, 3]
del messy[2]
Using parens acts like a function call.
Edgar Sánchez
508 PointsI dunno why this happens a lot with these code challenges here on Treehouse, I was having an error and I spent 10 minutes figuring out why, well if you could only correct the instructions as they explicitly ask you to delete the number 8, but for the challenge to pass you need to delete the 2.
Chris Freeman
Treehouse Moderator 68,454 PointsBe careful of syntax. The line
del messy[2]
refers to the index 2 (that is, the third element in a 0-based count) and not the integer object 2 (which could be removed using del messy[1]
)
Edgar Sánchez
508 PointsYou are totally right, I get it now!
Randell Purington
9,992 PointsThese codes are correct. If someone is like me, the space errors will drive you mad trying to figure out why you are getting it "Wrong" when your code is correctly written.
I had del messy [2]. everything looked correct, I read the comments on here and seen, I added a space. So the correct way to write it is del messy[2].
just a tip for the ones who are still lost when everything looks correct. check for spacing errors.