Python Python Collections (2016, retired 2019) Lists Removing items from a list

Pei Sheng Tan
Pei Sheng Tan
Python Web Development Techdegree Student 2,600 Points

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


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?
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:

1 Answer

Jennifer Nordell
Jennifer Nordell
Treehouse Staff

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: