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

Python Collection - Last Challenge Can't remove list item from list

for this challenge I wrote the following code: the_list = ["a", 2, 3, 1, False, [1, 2, 3]]

move_item = the_list.pop(3) the_list.insert(0, move_item)

removed = []

for item in the_list: if type(item) is bool or type(item) is str or type(item) is list: location = the_list.index(item) print (location) del the_list[location]

print(the_list)

The output I got was:

1 3 [1, 2, 3, [1, 2, 3]]

Process finished with exit code 0

For some reason the if statement doesn't catch the list item and puts its index in the location var. What am I doing wrong?

Thanks,

1 Answer

Steven Parker
Steven Parker
243,656 Points

:point_right: Skipped items can result from modifying the iterable inside a loop.

To make sure you check every item, use a copy of the list to control the loop:

for item in the_list[:]:  # slice without parameters creates a copy

Next time, please be sure to use the proper Markdown syntax for your code.

A newbie question, how do you add part of the code to the body of the question/comment?