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 trial

Python Python Basics (2015) Python Data Types List cleanup

Mason Lim
Mason Lim
1,209 Points

How 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.

lists.py
messy = [5, 2, 8, 1, 3]
del messy(2)

4 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

You'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
Edgar Sánchez
508 Points

I 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
Chris Freeman
Treehouse Moderator 68,423 Points

Be 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
Edgar Sánchez
508 Points

You are totally right, I get it now!

Randell Purington
Randell Purington
9,992 Points

These 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.