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

pls help

pls help
messy_list = ["a", 2, 3, 1, False, [1, 2, 3]]
item1 = messy_list.pop(3)
# 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
195,447 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.