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 trialYuda Leh
7,618 PointsBest way to move items? .remove() or .pop()?
So this is so a user can move an item from one location to another. At first before watching the Pop video I did it as such:
(using .remove())
def move_item():
show_list()
print("Type item name and positon to move to.")
item_name = input("Item name: ")
item_pos = int(input("Item position for item to move to: "))
spot = int(item_pos) - 1
shopping_list.remove(item_name)
shopping_list.insert(spot, item_name)
show_list()
After watching the Pop video (using .pop())
def move_item():
show_list()
print("Type item name and positon to move to.")
item_name = input("Item name: ")
item_pos = int(input("Item position for item to move to: "))
pos = shopping_list.index(item_name)
item = shopping_list.pop(index)
shopping_list.remove(item_name)
shopping_list.insert(index, item_name)
shopping_list.insert(pos, item)
So both methods produce the same output on a list. The .pop() method took a little more lines of code, because I have to store the popped item then add it to the position of the removed item.
I know the .remove() method is probably the better way, but I was not 100% sure if it was. I am also not sure if I went about the .pop() method correctly.
For the reason, I decided to ask the community. Thanks in advanced
1 Answer
Chris Howell
Python Web Development Techdegree Graduate 49,702 PointsHi Yehuda Lelah
So I would say neither is best really, it is dependent on what you need in a given situation. They both exist because they may be needed in different scenarios.
So like maybe sometimes you just want to remove the item in a list and you dont care to hold onto anything about that item, you just want it gone. .remove() will do that.
In another scenario you may want to remove an item but you dont want to lose the item you removed, you need to put into a new list of things or it needs to saved or used somewhere else. .pop() can do that.
You could also do this (i added some comments too)
def move_item():
show_list()
print('Type item name and position to move to.')
item_name = input('Item name: ')
item_pos = int(input('Item position for item to move to: '))
item_index = shopping_list.index(item_name)
# Since pop() removes an item from a list AND returns that item back.
# if all i am going to do is store it in a variable just to insert it back in
# Instead of this...
## item = shopping_list.pop(item_index)
## shopping_list.insert(item_pos, item)
# I could do this...
shopping_list.insert(item_pos, shopping_list.pop(item_index))
# it is basically the same.
Yuda Leh
7,618 PointsYuda Leh
7,618 PointsYeah, I never thought of that. Now I have a better understanding between the differenced of .pop() and .remove()! Thanks Chris