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

Extra Credit help. Python List Redux.

I am trying to create a command named CLEAR (def clear_list()) that deletes all items in a list, but am having trouble getting it to work properly. Please help.

shopping_list = []


def remove_item(idx):
  index = idx - 1
  item = shopping_list.pop(index)
  print("Remove {}.".format(item))

def clear_list():
  del new_list()
  print("Shopping list cleared.")

def show_help():
  print("\nSeparate each item with a comma.")
  print("Type DONE to quit, SHOW to see the current list, REMOVE to delete an item, CLEAR to delete entire list, and HELP to get this message.")


def show_list():
  count = 1
  for item in shopping_list:
    print("{}: {}".format(count, item))
    count += 1

print("Give me a list of things you want to shop for.")
show_help()

while True:
  new_stuff = input("> ")

  if new_stuff == "DONE":
    print("\nHere's your list:")
    show_list()
    break
  elif new_stuff == "HELP":
    show_help()
    continue
  elif new_stuff == "SHOW":
    show_list()
    continue
  elif new_stuff == "REMOVE":
    show_list()
    idx = input("Which item? Tell me the number. ")
    remove_item(int(idx))
    continue
  elif new_stuff == "CLEAR":
    clear_list()
    continue
  else:
    new_list = new_stuff.split(",")
    index = input("Add this at a certain spot? Press enter for the end of the list, "
                  "or give me a number. Currently {} items in the list.".format(
        len(shopping_list)))
  if index:
    spot = int(index) - 1
    for item in new_list:
      shopping_list.insert(spot, item.strip())
      spot += 1          
  else:
    for item in new_list:
      shopping_list.append(item.strip())

3 Answers

Looking at your code what your clear list function is doing is trying to delete a function called new_list.

I believe what you are trying to do is empty the list by deleting its contents.

A good way to do this is:

del shopping_list[:] # the : represents the contents of a list we went over this in string slicing.

#or as Charles suggested you could simply over write it with a new list

shopping_list = []

I hope that helps! --Ricky

thanks! that is very helpful to know. I assume that the colon in the brackets means that everything in the brackets is included in the command?

ie if [1, 2, 3, 4, 5] is a list then [:] would mean everything in that list (1 through 5)

You are correct!

Using list_name[:] gives you the whole contents of the list.

It is a string slice which grabs the whole list

list_name[:1] would give you the first and list_name[:2] will give you the first two.

You can checkout python collections for more information.

Goodluck! --Ricky

I am just starting collections and wanted to see if I could figure out the extra credit without moving on. Thanks for the help! The del shopping_list[:] worked as I wanted!

That's great to here! I believe the reason the other didn't work is because of something called scope. The shopping_list = [] inside of a function makes a new shopping_list that only exists in that function. To make it work as intended you would need to pass it into the function, overwrite it, return it, and reassign it. That was why i went with a different solution.

Goodluck! --Ricky

One way to tackle the clear function is to reset the shopping list to a blank list.

for example:

def clear list:

shopping_list = []

wow. i didn't even think of that! very simple solution. Thanks!

I tried your idea and it doesn't produce errors, but if I type SHOW after typing CLEAR it still has the items in the list.

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

You can always just call .clear() on the list.

>>> a_list = [1, 2, 3]
>>> a_list.clear()
>>> a_list
[]