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 trialGerald Bishop
Python Development Techdegree Graduate 16,897 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?
3 Answers
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! 🙂
Robbie Baker
3,700 PointsI just wanted more clarification on this. so by making a copy, Craig essentially has a new list he is popping items from. Can i assume the only reason it doesn't remove that item is because during each iteration it makes a new copy of wishes?
Evan Dix
1,597 PointsI beleive the purpose of the copy is to have a static (non-changing) list that can be used for the "For" loop to ensure the number of iterations in the "For" loop equal the number of objects in the list. The key point is that if you use the original list for the "For" loop, it's size will be changing every time the loop runs and will end up deleating every other value as Rohald described. Because the list in the for loop is the same as the list you are removing values from, To answer your question, we are not removing items from the copy, but the copy is used to ensure we take action on every index in the list.
Without the copy:
list = ["A", "B", "C"]
for letter in list:
list.remove(letter)
<first iteration results (index of list = 0) & (list[0] = "A")>
list.remove("A") = ["B", "C"]
<now this ["B", "C"] list gets used for the second iteration (idex of list = 1) & list[1] = "C">
list.remove("C") = ["B"]
<there is no value for the list idex of 2 and the loop is completed. A final list of [B] is returned>
This is not what we want. I hope this will help you visualize why we do not want to use the original list in the "For" loop.
Now let's use a copy of the list for the for loop:
list = ["A", "B", "C"]
for letter in list.copy():
list.remove(letter)
<first iteration results (index of list.copy() = 0) & (list.copy()[0] = "A")>
list.remove("A") = ["B", "C"]
<second iteration results (index of list.copy() =1) & (list.copy()[1] = "B")>
list.remove("B") = ["C"]
<third iteration results (index of list.copy() =2) & (list.copy()[2] = "C")>
list.remove("C") = []
This is exactly what we want. I hope this helps you visualize how using a copy of the list will ensure that we itteratively handle each item in a list.
Nicole Buckenwolf
8,721 PointsThank you Evan Dix that is exactly what I needed to help me get this.