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 trialMorten Egge
1,898 PointsFor loop stops
I have tried several combination's but when the for loop is finished there is still a list with integers left instide the list.
Anyone that knows whatβs wrong and whatβs going on ?
messy_list = ["a", 2, 3, 1, False, [1, 2, 3]]
# Your code goes below here
messy_list.insert(0,messy_list.pop(3))
for i in messy_list:
if not type(i) is int:
messy_list.remove(i)
2 Answers
Anibal Marquina
9,523 Pointsfor _ in range(0, len(messy_list)):
for i in messy_list:
if not type(i) is int:
messy_list.remove(i)
Morten Egge
1,898 PointsThank you.
Though I had already figured out that I needed a second loop outside. I still don't understand what's going on...
What do you mean "Have to use Remove and Del methods" ? "Hard Coding" the actual index numbers ?
Anibal Marquina
9,523 PointsHi.. yes basically hard coding the index..
Anibal Marquina
9,523 PointsAnibal Marquina
9,523 PointsHi there..
If you want to do it using loop logic you will have to be sure your loop runs until the end of your list adding for _ in range(0, len(messy_list)):
That works to overcome the challenge but you must try to do it using Del and Remove() methods, that will make you aware how the list mutates and how the index of the elements changes every time you erase an item from the list.