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

what is wrong with this code? it is supposed to change the list to have only integers in it

i also tried to check isinstance() separately with every type in the list 'bool' and 'str' and still it didn't work! One more time it is related to a module in python tutorials that i need to pass in order to be able to finish studying!

messy_list = ["a", 2, 3, 1, False, [1, 2, 3]] messy_list.insert(0, messy_list.pop(3))

Your code goes below here

for item in messy_list: if not isinstance(item, int): messy_list.remove(item)

4 Answers

salman khan
salman khan
1,283 Points

Can you please share you're code

messy_list = ["a", 2, 3, 1, False, [1, 2, 3]] messy_list.insert(0, messy_list.pop(3))

Your code goes below here

for item in messy_list: if not isinstance(item, int): messy_list.remove(item)

Chris Utley
Chris Utley
8,611 Points

Hi Ahmed,

Your next bit of code should look like this...

for item in messy_list.copy(): #<---- iterates over a copy of messy_list, this way when using "del" we won't delete indexes if type(item) != int: print(item) del messy_list[messy_list.index(item)]

salman khan
salman khan
1,283 Points

hi Ahmad, I think you're code should work for challenge 1

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

refresh you're page and try..

salman khan
salman khan
1,283 Points

hi Ahmad, for the challenge 2 you can use del keyword or remove keyword. del takes the index number as an argument for example

del messy_list[0]

and the remove keyword takes the values as an argument for example

my_list = ["a","b","c",]
my_list.remove("a") # this will remove 'a' from the list
my_list.remove("b") #this will remove 'b' from the list

make sure to remember keywords syntax's