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

pls help

pls help

lists.py
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) #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

2 Answers

Hi ! Look carefully at the indexes when you delete things, remember that if you delete an item, the index of item that was next to it has now decremented

Steven Parker
Steven Parker
231,275 Points

Whoever wrote in those comments had a good idea, you might want to use "remove" instead of "del" to avoid having to keep track of index numbers.

Another trick that might be helpful is using negative index numbers, which are relative to the end of the list instead of the beginning.