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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Joseph Michelini](https://uploads.teamtreehouse.com/production/profile-photos/11528572/micro_amtrappe_maxkulicke.jpg)
Joseph Michelini
Python Development Techdegree Graduate 18,692 PointsAfter reading similar questions and answers, I still don't understand why looping over the original list doesn't work.
Hey! I'm still having trouble understanding why ['sword', 'boomerang']
is left after trying to loop over the original inventory
list without making a copy. I do understand how we are using the copy of the list to mutate the original list, but why can't we use inventory.remove(item)
to remove all of the items in the original list?
Thanks in advance!
4 Answers
![Steven Parker](https://uploads.teamtreehouse.com/production/profile-photos/969492/micro_tool_chess_set_2.jpg)
Steven Parker
230,688 PointsModifying the same list that you are using to iterate with changes the indexes of the items. This causes the loop to skip over the next item in the sequence.
But using a copy ensures that each item will be seen by the loop.
![Tiffany Greathead](https://secure.gravatar.com/avatar/04559e0d2119d4c804449d553682419f?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Tiffany Greathead
6,613 PointsRajat Tomar, it helped me thinking of it this way inventory = [0 "shield", 1 "sword", 2 "bow", 3 "boomerang"] (the numbers represent their index position)
When you use the "for" loop, it reassigns the index:
removes 0 (shield)
inventory = [0 "sword", 1 "bow", 2 "boomerang"]
loops again
removes 1 (bow)
inventory = [0 "sword", 1 "boomerang"]
Regardless of the change in indexes, the loop is still going to loop through each of the items in order from 0 - current len(inventory). The loop stops at one because the next index it would loop through is 2, but there is no index 2. Hope this makes sense.
![Siddharth Pathak](https://secure.gravatar.com/avatar/564ef7ce0c8b4a53fda0378f7aa599d0?s=60&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Siddharth Pathak
Python Development Techdegree Student 74 Pointsthis is the best explaination i even seen veery good tiffanygreathead
![Tiffany Greathead](https://secure.gravatar.com/avatar/04559e0d2119d4c804449d553682419f?s=60&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Tiffany Greathead
6,613 PointsThank you Siddharth Pathak, I'm glad you found it helpful
![heebers](https://uploads.teamtreehouse.com/production/profile-photos/11662519/micro_22CBA76D-1147-4132-A5A3-D2EAF1EE6E84.jpg)
heebers
Python Development Techdegree Student 1,835 Points1 year later, but thank you!!!!
![cheeseslope](https://uploads.teamtreehouse.com/production/profile-photos/11573416/micro_cheeseslope.jpg)
cheeseslope
3,303 PointsI wrote this program to help myself visualize what’s happening here. It uses the index() method to output the removed item’s index location during the loop.
bugs = ["grasshopper", "cricket", "locust", "beetle", "spider"]
for bug in bugs:
bug_index = bugs.index(bug)
print(f"List is now → {bugs}")
print("") # Blank line
print(f"Removing '{bug}' at index {bug_index}")
bugs.remove(bug)
print(f"List is finally → {bugs}")
print("") # Blank line
print("Bugs squashed! (Not really, though.)")
Output:
List is now → ['grasshopper', 'cricket', 'locust', 'beetle', 'spider']
Removing 'grasshopper' at index 0
List is now → ['cricket', 'locust', 'beetle', 'spider']
Removing 'locust' at index 1
List is now → ['cricket', 'beetle', 'spider']
Removing 'spider' at index 2
List is finally → ['cricket', 'beetle']
Bugs squashed! (Not really, though.)
![Joseph Michelini](https://uploads.teamtreehouse.com/production/profile-photos/11528572/micro_amtrappe_maxkulicke.jpg)
Joseph Michelini
Python Development Techdegree Graduate 18,692 PointsThat makes sense! Thank you Steven.
brokemotivation
Python Development Techdegree Student 1,982 Pointsbrokemotivation
Python Development Techdegree Student 1,982 PointsStill couldn't get it. Can you please elaborate?