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

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

Philip Schultz
Philip Schultz
11,437 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,061 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)
Philip Schultz
Philip Schultz
11,437 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

Philip Schultz
Philip Schultz
11,437 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

Philip Schultz
Philip Schultz
11,437 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,061 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 ;)