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 trialTyler Mootz
2,972 PointsWhy use .pop() or .copy() methods instead of using an index reference?
My code is working fine and I understand how Craig's use of the methods work here, but I don't understand why we wouldn't just assign the "wishes" zero-index instead of popping it from the list itself?
Is this because if we don't pop it, then the formatting for the rest of the list will also apply to the first item?
1 Answer
Jennifer Nordell
Treehouse TeacherHi there, Tyler Mootz! Yes, if you didn't .pop()
the item it would still be remaining in the list when you started the for
loop which means it would appear both as the suggested item and the first item in the following list. The .copy()
is so that you aren't altering the thing you're iterating over as you're iterating, which can cause some skipped checks to happen.
Hope this helps!
Tom Ireland
4,191 PointsTom Ireland
4,191 PointsYes, I had a similar thought as Tyler, so changed the code from
wishes.pop(0)
towishes[0]
(before Craig explained further). But, as you pointed out Jennifer Nordell , it displays the item twice.