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 trialsowmya c
6,966 Pointsremove from list
remove everything from the list except integers
messy_list = ["a", 2, 3, 1, False, [1, 2, 3]]
item1 = messy_list.pop(3)
messy_list.insert(0,item1)
# Your code goes below here
del messy_list(1)
del messy_list(4)
del messy_list(5)
3 Answers
Antonio De Rose
20,885 Points#there are 2 issues in your code
#before that, try the code window, just as you have done the very first time, it makes the readability high,
#and, when we paste your code, we do not have indent it.
messy_list.remove(a) #are you removing a variable or a string
messy_list.remove(False)
del messy_list(3) #I already mentioned, you cannot use this bracket.
Antonio De Rose
20,885 Pointsmessy_list = ["a", 2, 3, 1, False, [1, 2, 3]]
item1 = messy_list.pop(3)
messy_list.insert(0,item1)
# Your code goes below here
del messy_list(1) #you cannot use this bracket
del messy_list(4) #you cannot use this bracket
del messy_list(5) #you cannot use this bracket
#I would suggest you to use, remove, as the list is being popped and inserted on the run
#would be handy to use remove for the values false and "a"
#then del for the list inside
#you will get a list index out of range error, as there isn't an index 5, after false and "a"
#have been taken off.
#do a little bit of counting while you use remove, pen and paper itself will become handy,
#and then put the appropriate index for the del
sowmya c
6,966 Pointsmessy_list = ["a", 2, 3, 1, False, [1, 2, 3]] item = messy_list.pop(3) messy_list.insert(0,item) messy_list.remove(a) messy_list.remove(False) del messy_list(3)
I wrote this code but it gives an error
Krishna Pratap Chouhan
15,203 Pointschange in index is causing this problem.
a similar, For loop example...
sowmya c
6,966 Pointssowmya c
6,966 PointsThanks.It makes sense now