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

Why do my items come out in reversed order when inserted at a particular index? (Shopping List 3)

I followed the video and this is my code:

shopping_list = []

def show_help():

    print("\nSeperate each item with a comma.")
    print("Type DONE to quit, SHOW to see the current 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 is your list:")
        show_list()
        break
    elif new_stuff == "HELP":
        show_help()
        continue
    elif new_stuff == "SHOW":
        show_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())

Below is the result I got when I tried to insert "milk, cream" into the list at index 1 (actually 0). Instead of getting 1. milk and 2. cream, I got 1. cream and 2. milk. How is that possible??

and HELP to get this message.                    
> apple, bananas, yogurt                         
Add this at a certain spot? Press enter for the e
nd of the list, or give me a number. Currently 0 
items in the list.                               
> milk, cream                                    
Add this at a certain spot? Press enter for the e
nd of the list, or give me a number. Currently 3 
items in the list. 1                             
> SHOW                                           
1: cream                                         
2: milk                                          
3: apple                                         
4: bananas                                       
5: yogurt                                        
> DONE                                           

Here is your list:                               
1: cream                                         
2: milk                                          
3: apple                                         
4: bananas                                       
5: yogurt

[MOD: added ``` block markdown formatting -cf]

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

The issue is spot is incremented outside the for loop, so all items are added at the same spot shifting over earlier adds.

         "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  # <-- indented inside of for loop

AH I see! Thank you Chris!