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 Collections (2016, retired 2019) Lists Removing items from a list

Pei Sheng Tan
seal-mask
.a{fill-rule:evenodd;}techdegree
Pei Sheng Tan
Python Web Development Techdegree Student 2,600 Points

for loop to check element class in list did not work as expected

Hello!

So the code doesn't remove the list of integers at the end, even though it removed all the other non integers.

Shouldn't the for loop remove the list also, since it's a list and not an int, despite the list containing integers?

lists.py
messy_list = ["a", 2, 3, 1, False, [1, 2, 3]]

# Your code goes below here
messy_list.insert(0, messy_list.pop(3))
ref_messy_list = messy_list
for i in ref_messy_list:
    if type(i) != int:
        messy_list.remove(i)

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, Pei Sheng Tan !!! You are terribly close here. And you sort of hint at the problem in your naming of the variable ref_messy_list which, in fact, does contain a reference to the original list. So if you change, the original list, you change the reference and vice versa. What is happening is that last check is getting skipped entirely because the indexing is getting messed up.

I have a feeling what you meant to do was make a copy of the original list.

So where you wrote:

ref_messy_list = messy_list

You should have:

ref_messy_list = messy_list[:]

The latter makes a copy so that the two are no longer "linked" really. Hope this helps! :sparkles: