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

Cherie T
Cherie T
3,429 Points

Can't use .remove() to remove the list from inside of messy_list. Why?

Hi there. Going through Challenge Task 2 of 2 in Python Collections on Lists.

For some reason, my code can remove the bool and str but not list... only when using messy_list.remove[1,2,3] does it remove the list. Why is that?

Thanks!

    messy_list = ["a", 2, 3, 1, False, [1, 2, 3]]

    # Your code goes below here

    messy_list.insert(0,messy_list.pop(3))


    for item in messy_list:
        if item is not int:
            messy_list.remove(item)
    messy_list.remove[1,2,3] 

1 Answer

Steven Parker
Steven Parker
172,377 Points

Removing items from an iterable while it is controlling a loop can cause other items in the loop to be skipped over. To prevent this, use a copy of the iterable to control the loop.

Also, I wouldn't expect "item is not int" to be a viable test. Did you mean to use the "type" function?

P S
P S
10,184 Points

Hey Steven, Can you expand on this? How would you use a for loop to get rid of certain data 'types'. I can't seem to get it to work. This is what I have.

for item in copy_list:
    if item != type(int):
        messy_list.remove(item)
Steven Parker
Steven Parker
172,377 Points

You'd want to put the "type" function on the item. And you can use the identity operator here:

    if type(item) is not int:
P S
P S
10,184 Points

Hey Steven, Thanks for the help. When I made the adjustment to the code it deleted the bool and the string but left the list. Why is this? Also, is there a rule of thumb on when to use identity operators?

Thanks again for your help!

Steven Parker
Steven Parker
172,377 Points

You didn't show the code where "copy_list" is created. I'm suspecting it might not be a copy but just another reference to the list and the skipping over might still be taking place.

A good example use of the identity operator would be checking my assumption (before the loop):

if copy_list is messy_list:
    print("these are both names for the SAME list")
elif copy_list == messy_list:
    print("these are copies, good")
else
    print("these are entirely different lists")
P S
P S
10,184 Points

Here is my full code

messy_list = ["a", 2, 3, 1, False, [1, 2, 3]]
copy_list = messy_list
print(messy_list)
for item in copy_list:
    if type(item) is not int:
        messy_list.remove(item) 
print(messy_list)

I actually haven't tried this for the treehouse challenge, I'm using my Geany editor and printing the output to my console. I'm positive that it isn't every other one though- it is printing all of the integers that are in consecutive order. The output I'm getting is [2, 3 ,1, [1, 2, 3]]

********You're right, I wasn't copying the list correctly**********************

messy_list = ["a", 2, 3, 1, False, [1, 2, 3]]
copy_list  = messy_list.copy()
print(messy_list)
for item in copy_list:
    if type(item) is not int:
        messy_list.remove(item) 
print(messy_list)
Steven Parker
Steven Parker
172,377 Points

Yup, I guessed it, that's just a reference to the same list. You can confirm it with the little test program in the previous example. To get a copy of the list you can do this.

copy_list = messy_list.copy()  # or messy_list[:]
P S
P S
10,184 Points

Awesome! Thanks for your help!