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 Build a Soccer League

Alex Tirrell
Alex Tirrell
3,359 Points

Why is my program only detecting six out of the nine experienced players?

The first thing my program does is separate the players into two lists, experienced and not experienced. In order to do so , I use the code:

for player in players: if player['Soccer Experience'] == 'YES': experienced.append(players.pop(players.index(player)))

For some reason, this is only transferring six out of the nine players. Why might that be happening?

2 Answers

Steven Parker
Steven Parker
229,783 Points

:point_right: You should never alter a list that is being iterated, items will be skipped.

Instead, always iterate using a copy of the list you will be altering. You can easily make a copy using a slice with no parameters:

    for player in players[:]:
Alex Tirrell
Alex Tirrell
3,359 Points

Ohh, I see! It was moving on to the next index of the list, but because something had just been removed, it was causing it to actually skip the next item! That makes a lot of sense, thank you.