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

Bapi Roy
Bapi Roy
14,237 Points

What is wrong with my code

messy_list = ["a", 2, 3, 1, False, [1, 2, 3]] messy_list2 = ["a", 2, 3, 1, False, [1, 2, 3]]

Your code goes below here

for i in messy_list2: if type(i) is not int: messy_list.remove(i)

Output need to be a list (messy_list) with integer

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

# Your code goes below here
for i in messy_list2:
    if type(i) is not int:
        messy_list.remove(i)

print messy_list

6 Answers

Bapi Roy
Bapi Roy
14,237 Points

Thank you for your reply.

I am still getting "Oops! " in Challenge Task.

The task is "Great! Now use .remove() and/or del to remove the string, the boolean, and the list from inside of messy_list. When you're done, messy_list should have only integers in it."

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

You must still have some values in there you're not expecting. Try it out in your Python shell on your computer or in Workspaces.

Bakthyar Syed
PLUS
Bakthyar Syed
Courses Plus Student 884 Points

messy_list.insert(0,messy_list.pop(3)) for item in messy_list: if type(item) is not int: messy_list.remove(item) messy_list.pop(-1)

Bapi Roy
Bapi Roy
14,237 Points

In my shell, I am getting no error. But is Treehouse's Challenge Task page I am getting "Oops! " message.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

When I run your code, with the print fixed, I get a very specific error message in the code challenge. You've missed a step in the instructions.

Bapi Roy
Bapi Roy
14,237 Points

@keenethlove you were right, I was missing a point. Thank you

David Leal
David Leal
1,990 Points

hey Kenneth Love i am wondering why we cant do this messy_list = ["a", 2, 3, 1, False, [1, 2, 3]] for i in messy_list: if type(i) is not int: messy_list.remove(i) i mean why we have to use a second list

Bapi Roy
Bapi Roy
14,237 Points

@Kenneth It is working fine in my system shell, yet I am not able to pass the challenge.