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!
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
Gerald Bishop
Python Development Techdegree Student 5,290 PointsHow does copy() help to access each item in the original list to delete them?
Hi, I understand that looping through the original list and deleting items shifts the remaining items along and then some get missed if you want to delete all items.
I don't understand how using a copy of the list prevents that problem. What happens when a for loop iterates through a copy that doesn't happen when it iterates through an original list that is also being deleted?
1 Answer

Rohald van Merode
Treehouse StaffHey Gerald Bishop 👋
When you don't use the copy()
method every time the item is deleted, the other items in the list shift from their original index numbers. Because of this, half of the items remain when you delete them from the same list you're looping over. The first item is deleted on the first iteration, making the second item move to the first position. When the loop moves on to the next iteration, the list's second item (originally in the third position) is removed from the list.
If you would create a copy of the original list and loop over that instead, your loop will run over each item in that copy. Since you're not modifying the copy but the original list, you're no longer running into the same issue of only half of the items being deleted.
I hope this clears things up! 🙂