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

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

# Your code goes below here


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.