Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Collections (Retired) Lists Redux Removing list items

my_list=[1. 4, 2, 3] i want to delete the 4 my from my_list using del.

i want to delete 4 my from my_list using del.

6 Answers

I know nothing about Python, but are you supposed to have a period or comma after the 1?

Hi Ephraim,

When using del you have to specify the index of the value you want to remove.

Example:

del some_list[5] # This would remove the value that is at index 5 from some_list
Jane Chester
Jane Chester
5,411 Points

The delete function using square [] brackets looks at the placement of the number. The 4 is located at index 1.

# my_list = [1, 4, 2, 3]

del my_list[1] 
Darren Joy
Darren Joy
19,573 Points

I would go with what Ted Sumner said.

i.e. If you are deleting, it chooses what to delete by index, the 1. (period aka dot) in the list [1. 2, 3, 4] is likely going to mess up the count for the indexing.

Stat by fixing that... to a comma... then...

as Jason Anello said, you then have to direct it to the 4 in your del, remember, count or items starts at 0

note: edited it remove answer, which was off anyway

The 1. was just a typo when restating the quiz question. The quiz question has the correct syntax. I didn't state the full answer because I think we should let people try to figure out at least part of the answer on their own.

Index 3 isn't correct though.

Another thing that you might find is that the first and second number may be ignored because it might be read as a decimal rather than two distinct values. I think that Ted Sumner's response is correct as well.

I don't think the period is the issue here. It's not in the actual code.

This question is from the quiz:

my_list = [1, 4, 2, 3]

I want to delete the 4 my from my_list using del.

There's no period after the 1. I think that there was just a typo when the quiz question was retyped for this post.

There is a problem with typos in a question that requires exact syntax.

A quick search came up this this answer, although I still know nothing about python:

del my_list[1] This deletes the from the index position

or

del my_list(4) This deletes the value.

See this stackoverflow.com answer.

When using del you have to specify the index. Your 1st example would work but not the 2nd one.

# doesn't work
del my_list(4)