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

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)

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