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 Collections (Retired) Lists Redux Manipulating Lists

How do i remove items from my list using del and or/ .remove?

Use .remove() and/or del to remove the string, boolean, and list members of the_list.

lists.py
the_list = ["a", 2, 3, 1, False, [1, 2, 3]]

# Your code goes below here
the_list.pop(3)
the_list.insert(0, 1)

the_list.remove("a")
del the_list

don't use #1 in list

remove.(1)put the item in ()

12 Answers

thank you Tatiana

I advanced past this challenge by removing based on value or index. It's not very algorithmic, though. I tried a for loop that tested the item type and wasn't successful initially, but I just tried running this from the console:

for item in the_list:
  if type(item) is not int:
    the_list.remove(item)

For some reason, the first time through removes every non-int item except the [1,2,3] list object; running it the second time removes the list object. Let us know how you solve it! If anyone has any ideas on why this doesn't remove the list object on the first pass but does on the second pass, let me know.

the_list.remove([1,2,3])
Jullien Selby
Jullien Selby
16,320 Points

Here is how I solved it using del.

messy_list = ["a", 2, 3, 1, False, [1, 2, 3]]
# Your code goes below here

messy_list.pop(3)
messy_list.insert(0, 1)
messy_list.remove("a")
del messy_list[-2:]
print messy_list ```

thanks it worked, i used the_list.remove[1,2,3] i don't know why del didn't work though? any specific reason?

Did you use the index of the item in the list? Remember, remove works based on value (so -- .remove("a")), whereas del works based on index in the list (so -- del the_list[0]). If you share your code, it's easier to troubleshoot.

yes i used the index but its not working

Hmm. I'd suggest playing around with it in the console, since that seems to provide better error messages than the code challenge. When you use it, ask:

Do you get a syntax error? Are you able to remove another item from a list? (Or an item from another list?) Are you using the correct index? Remember, Python starts its index at 0. Does it remove some other item from the list instead? (This probably means that you have the wrong index.) Remember, you can also use the .index function in Python to find the index. After you remove an item, that will change the index of other items in the list...

Hope that helps!

Yup nice i got it !! you are awesome thanx i was messing up the index. Thanx again

Kody Dibble
Kody Dibble
2,554 Points

Sooo, I'm trying to...

the_list.remove("a") the_list.remove(1,2,3)

does remove not work for int's?

Hi Kody, i think it does

Hi Kody, i think it does

Blake Handson
Blake Handson
1,642 Points

This works. All the best.

the_list = ["a", 2, 3, 1, False, [1, 2, 3]]

Your code goes below here

var1 = the_list.pop(3) the_list.insert(0, var1)

the_list.remove("a") the_list.remove(False) the_list.remove([1,2,3])

Bakthyar Syed
PLUS
Bakthyar Syed
Courses Plus Student 884 Points

messy_list.insert(0,messy_list.pop(3)) for item in messy_list: if type(item) is not int: messy_list.remove(item) messy_list.pop(-1)

print(messy_list)
messy_list.remove("a")
messy_list.remove(False)
messy_list.remove([1, 2, 3])

the_ list.remove{1,2,3) don't use the #1