Python Python Collections Lists Removing items from a list

Bummer: Looks like you still need to remove some items from `messy_list`.

i del the string value and boolean value but still sying still need to remove

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

# Your code goes below here
messy_list.insert(0, messy_list.pop(3))
del messy_list[1]
del messy_list[3]

5 Answers

Henrik Christensen
Henrik Christensen
35,790 Points

You also need to remove the list [1, 2, 3]

P S
P S
10,180 Points

Hey Ali, Task 1 looks good. For task two it doesn't look like you are deleting all of the correct values. Remember that 'list' is it's own data type. Even if a list contains all integers it still considered a list. So delete the list.

Also, someone was helping me the other day make a for loop that quickly went through the list and deleted everything that wasn't an 'int' data type. Check out the solution below and let me know if you have questions.

loop_list = messy_list.copy()
for item in loop_list:
    if type(item) is not int:
        messy_list.remove(item)
Ross Coe
Ross Coe
5,059 Points

is not int doesn't work - just removes all items in a test file My solution:

messy_list.insert(0, messy_list.pop(3))
loop_list = messy_list.copy()
for item in loop_list:
    if type(item) != int:
        messy_list.remove(item)
P S
P S
10,180 Points

Ross, the code works just fine for me. What error are you getting? What do you mean about removing items in a test file? Can you substantiate your claim as to why using the '!=' is better than using 'is not' or why 'is not doesn't work'? Why do you think checking the value is better than checking the identity? Below is a StackOverflow post about how to check for object types, why do you think 'is' works for them?

https://stackoverflow.com/questions/2225038/determine-the-type-of-an-object

del messy_list[1] #for remove "a" and then click next del messy_list[3] # for remove (Boolean) and then click next del messy_list[3] # for remove [1, 2, 3,] and then click next

P S
P S
10,180 Points

Remember that you changed the state of your list in step 1 of 2 by putting what was in the 3 index into the 0 index. So, you are correct about the character 'a' at the index of 1. However, when you delete that index you're changing the state of the list again, everything will shift to the left and what was once at index 2 will now be at index 1. . If you insist on using the del function you will have to keep track of the index as you delete them. Open up workspaces and run this code.

messy_list = ["a", 2, 3, 1, False, [1, 2, 3]]
copy_list = messy_list.copy()

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

del messy_list[1]
print(messy_list)
del messy_list[3]
print(messy_list)
del messy_list[3]
print(messy_list)

Notice how everything is shifting when you delete indexes? This isn't too bad working with such a short list, but imagine working with a huge list.....this would take forever. Now take a look at the for loop I made earlier and notice how that code can handle list with an infinite number of indexes.

copy_list = messy_list.copy()

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

del messy_list[1]

print(messy_list)

del messy_list[3]

print(messy_list)

del messy_list[3]

print(messy_list)

You don't need to print them

P S
P S
10,180 Points

Hey Jon, I wasn't suggesting for him to use that code for the challenge. I suggested for him to run that code in workspaces so he can see what is happening to the list as he deletes indexes. I thought I made that clear in the post.

My apologies. It definitely is killer code

Ross Coe
Ross Coe
5,059 Points

I pilfered your code and tried it several times in the quiz console below question - kept getting an error so I tested in a workspace and I got back a blank list (I know your 'is not' should work) so then I googled is not of type and when I modified it worked both in workspace and quiz console - some people as getting inconsistent errors in the quiz consoles - on another occassion it simply was that I hadn't closed out a function by making sure I return over to the gutter - so python thought I was still defining the function - I'm only telling your my experience not saying your wrong ;)