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

sowmya c
sowmya c
6,966 Points

remove from list

remove everything from the list except integers

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)
del messy_list(4)
del messy_list(5)

3 Answers

Antonio De Rose
Antonio De Rose
20,853 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.
sowmya c
sowmya c
6,966 Points

Thanks.It makes sense now

Antonio De Rose
Antonio De Rose
20,853 Points
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
sowmya c
sowmya c
6,966 Points

messy_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