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 Introducing Lists Using Lists Mutability

Joseph Michelini
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Joseph Michelini
Python Development Techdegree Graduate 18,692 Points

After 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
Steven Parker
231,268 Points

Modifying 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.

Still couldn't get it. Can you please elaborate?

Rajat 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.

this is the best explaination i even seen veery good tiffanygreathead

Thank you Siddharth Pathak, I'm glad you found it helpful

1 year later, but thank you!!!!

I 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.)