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
Tiger Rahman
1,333 PointsRemoving items from the list
My list is working just fine but curious about a better way to have a user remove a list. I had a friend try it out (who doesn't code) and the first thing she asked was "How do I fix a mistake I made?" Fair enough.
I added a few things:
review the list before deciding whether or not to continue
def review_list():
print_list()
print("Add more items to your list. Type 'DONE' when finished.")
be able to remove an item
elif new_item == 'REMOVE':
print_list()
trash = input("What would you like to remove? ")
shopping_list.remove(trash)
review_list()
continue
This all works just fine, but I can't help but wonder how this would best work to allow the user to more explicitly state what they wanted to remove (using the actual list index). I don't know that this is functionally necessary for this case, but if sequence mattered for some reason ("Apples must be item 5 on the list!"!) then there's no way to support this kind of neurosis -- except I'm sure there is.
Thoughts?
2 Answers
Chris Freeman
Treehouse Moderator 68,468 PointsAre you looking to enable removing items by index or by name? You can use enumerate in print_list to output items and indexes then if trash is int, pop(trash), otherwise use remove
If you wish to keep existing item at current indexes (apples st #5) then replace deleted item with shopping_list[index(trash)] = None or shopping_list[trash] = None If index provided by user.
You can test for user providing input using:
try:
# covert to int
Idx = int(trash)
# code here using idx as index
except:
# not int, treat as item
What else were you looking for?
Jeff Wilton
16,646 PointsYou are on the right track, and I applaud you (and your friend) for thinking beyond the given assignment. I encourage you to go to the source (so to speak) and look at the official Python documentation on data structures:
https://docs.python.org/3.6/tutorial/datastructures.html
Something like this might be what you are looking for:
list.pop([i])
This will remove the item at the given position in the list, and return it (though you don't need to do anything with the returned value).
I think if this application had a fancy user interface, I would probably want to click on a specific item in order to remove it from the list, and therefore the index is what I would recommend using to remove (pop) the item from the list. I suppose even in a text-based UI it would still be easier to type in an index rather than the whole name of the item to remove.
Hope this helps!
Tiger Rahman
1,333 PointsTiger Rahman
1,333 PointsSolid. Ran into a few things but checking out documentation helped! [https://docs.python.org/3.4/tutorial/errors.html] [https://docs.python.org/2/library/functions.html?highlight=enumerate#enumerate]
Might look into the caps-sensitivity later but learned a lot from this for today. Thanks for the assists!