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

remove

What am I doing wrong?

lists.py
messy_list = ["a", 2, 3, 1, False, [1, 2, 3]]

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

del messy_list("a")
del messy_list(False)

1 Answer

KRIS NIKOLAISEN
PRO
KRIS NIKOLAISEN
Pro Student 48,728 Points

If you run this in a workspace you'll receive a better error message:

    del messy_list("a")                                                                               
       ^                                                                                              
SyntaxError: can't delete function call        

So first instinct may be to switch to square brackets - but no:

    del messy_list["a"]                                                                               
TypeError: list indices must be integers or slices, not str    

Finally use integers for list indices:

del messy_list[1]
del messy_list[3]
del messy_list[3]  

Thank you it worked.