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

Frank Sherlotti
PLUS
Frank Sherlotti
Courses Plus Student 1,952 Points

Allowing items to be moved in a list from one position to another

I don't know where to start and i really don't know how to word this question but I'm going to try my best. Can I have an "item" inside of a for loop popped out and then inserted back in where the user inputs it in? I hope that makes some sense to someone smarter than me because currently I'm completely confused. And if that does work could I have an example of what it looks like, because I really don't quite understand how to right it out in code form. I attached a little snippet of what I think the code should start off looking like but I don't know where to go from this current point in the code, thanks.

def move_item():
  for item in shopping_list:

elif new_stuff == "MOVE":
    move_item()
    continue    

2 Answers

Michael Norman
PLUS
Michael Norman
Courses Plus Student 9,399 Points

If I understood the question correctly this is certainly possible, though I'm not sure a for loop would be the best way to handle it. I will first give you my solution in pseudo code in case you want to work it out yourself and at the bottom will be my actual solution.

def move_item():
    show_list() #Show the list
    # get input from the user for which item the want to move,  integer form
    # get input from the user for where they want to move the item, integer form
    # save that item from the shopping_list to a variable 
    # delete that item from the shopping_list, remember indices start at 0 in a list.
    # insert that item in shopping_list at the specified index
    show_list() # show the list again

elif new_stuff == "MOVE":
    move_item()
    continue  

My quick solution is below. It's slightly different than the pseudo code above but should be easy enough to follow. If you have any questions at all, please don't hesitate to ask!

def move_item():
    show_list()
    item_to_move = int(input("Which item would you like to move (enter the number): "))
    where_to_move = int(input("What position do you want it in (enter the number): "))
    shopping_list.insert(where_to_move, shopping_list[item_to_move - 1])
    del(shopping_list[item_to_move - 1])
    show_list()

elif new_stuff == "MOVE":
    move_item()
    continue
Frank Sherlotti
Frank Sherlotti
Courses Plus Student 1,952 Points

Ok i tested the your solution and it does kind of what i wanted it to do. I might have worded the question wrong or I just didn't explain it correctly. Say the list is 1. bread 2. eggs 3. bacon 4. milk 5. apples. And we want to move 5. apples to the top of the list instead of bread. Yours will take 5. apples and leave it at 5. and also move it to the top of the list as it slides the rest down it completely removes number 4. milk. What I was looking for was basically to swap items in a list I guess. Like take 5. apples and move it to the top and move 1. bread to where 5. apples was prior. Sorry if I worded that first question incorrectly. Or instead of swapping the items 5. apples and 1. bread just slide them all down without deleting one of the items in the list. Like make 1. apples then the rest in order from there. I hope I'm still making sense at this point I'm sorry.

Frank Sherlotti
PLUS
Frank Sherlotti
Courses Plus Student 1,952 Points
def move_item():
    show_list()
    item_to_move = int(input("Which item would you like to move (enter the number): "))
    where_to_move = int(input("What position do you want it in (enter the number): "))
    shopping_list.insert(where_to_move, shopping_list[item_to_move - 1])
    del(shopping_list[item_to_move])
    show_list()

I figured out how to make it do what I wanted it to do. It deletes the item_to_move and moves it to the desired spot and deletes Its old position instead of deleting the item prior in the list.