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

Lists Extra Credit Python

This is the shopping_list project part 3.

I tried to remove items from the list by slicing. There is something wrong with how I wrote the slice part. Can someone please tell me what that is? thank you kindly

remove_from_list():
    show_list()
    #ex. shopping_list = [apple, peach, kiwi]
    #1. apple
    #2. peach
    #3. kiwi
    what_to_remove = input("Please enter which item # you want to remove.")
    try: 
        what_to_remove = int(what_to_remove)
        shopping_list[what_to_remove-1:what_to_remove]

    except ValueError:
        print("Hey your item wasn't on the list or you didn't enter a # !")
        pass

2 Answers

When you want to use slice to remove items, you have to slice up to that item and then add everything after that item:

shopping_list = shopping_list[:what_to_remove] + shopping_list[what_to_remove+1:]

I think a cleaner way is to just pop the item at that index, then you can return that item to the user:

removed_item = shopping_list.pop(what_to_remove)
print("You removed " + removed_item + " from your list")

The cleanest way would probably be to have the user input the name of the item they want to remove and not the index:

# shopping_list.remove("apple")
shopping_list.remove(what_to_remove)

I tried both of these methods and both didn“t work.

shopping_list = shopping_list[:what_to_remove] + shopping_list[what_to_remove+1:]

This method doesn't work because I think we have to enter the index # in order to pop that item out.

removed_item = shopping_list.pop(what_to_remove)
print("You removed " + removed_item + " from your list")

So I tried to get an index # of the item that I wanted to remove from shopping_list but it resulted in an error which I can't figure out why.
for i in range(shopping_list): TypeError: 'list' object cannot be interpreted as an integer I figure i is just the indexes of shopping_list?

def remove_from_list():
    #1. show list
    #2. try to remove it
    #3. if a ValueError comes up, just past it
    show_list()
    what_to_remove = input("Please enter which item you want to remove.\n ")
    #shopping_list = shopping_list[:what_to_remove] + shopping_list[what_to_remove+1:]
    for i in range(shopping_list):
        if shopping[i] == what_to_remove:
            removed_item_index = i

    removed_item = shopping_list.pop(removed_item_index)
    print("you removed {}".format(removed_item))
    #shopping_list.remove(what_to_remove)


    #try: #try
        #shopping_list.remove(what_to_remove)
        #what_to_remove = int(what_to_remove)
        #shopping_list[what_to_remove-1:what_to_remove]

    #except ValueError:
        #print("Hey your item wasn't on the list!")
        #pass
    show_list()

Wow I'm surprised that you have to do this. shopping_list = shopping_list[:what_to_remove] + shopping_list[what_to_remove+1:]

I figure the whole point of slicing was making it simple and short. If we have to do it this way then when does slicing even exist if we can just use pop or remove method?

I personally use slicing all of the time. It's typically not best for removing a single item from a list, but it's very powerful when you need to operate on multiple parts of a list at the same time, remove whole sections of a list and when you start writing recursive functions you'll use slicing a lot more. Slicing is also used a ton when performing operations on strings.

You're getting the list error because when you loop you need to use len():

for i in range(len(shopping_list))

With that being said, you don't need the loop. You're correct, pop() takes an index number and remove() takes a string that matches an item in the list.

I removed all comments and you might want to add an if else statement for your print confirmation, I just want to demo the removal of the item. I tested all of these and they worked for me on my machine.

pop() method

def remove_from_list():
    show_list()

    #Expect index number
    index_to_remove = input("Please enter which item number you want to remove.\n ")
    removed_item = shopping_list.pop(int(index_to_remove))

    print("you removed {}".format(removed_item))

    show_list()

remove() method

def remove_from_list():
    show_list()

    # Expect string of item ex. "apple", "peach", "kiwi"
    item_to_remove = input("Please enter which item number you want to remove.\n ")

    shopping_list.remove(item_to_remove)

    print("you removed {}".format(item_to_remove))

    show_list()

Slice Method

def remove_from_list():
    show_list()

    # Expect index number
    index_to_remove = input("Please enter which item number you want to remove.\n ")

    index = int(index_to_remove)
    removed_item = shopping_list[index]
    shopping_list = shopping_list[:index] + shopping_list[index+1:]

    print("you removed {}".format(removed_item))

    show_list()