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

need help on how to delete every member of a list at once

need help on how to delete every member of a list at once

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(0:1:2:3:4:5:6:7);

3 Answers

Are you looking to delete the list from existence or delete each item from the list so you are left with an empty list?

To expand on John's response.

To delete all the items in a list the easiest method is just assign the variable to an empty list.

>>> g = [1, 2, 3]
>>> g
[1, 2, 3]
>>> g = []
>>> g
[]

To delete the list entirely you can use

>>> g = [1, 2, 3]
>>> del(g)
>>> g
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'g' is not defined

For the challenge linked in this question, you may need to know that you can use del() to delete an item in a list. You do this by giving del() the list and index number. For instance, to delete the 2nd item of a list named 'some_list' you would use, del(some_list[1]).

i tried this method but it does not work, thanks anyways guys.