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

Noah Harness
Noah Harness
1,383 Points

Eliminate two items in a list with either one or both del. and .remove()

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.

lists.py
messy_list = ["a", 2, 3, 1, False, [1, 2, 3]]
messy_list.insert(0, messy_list.pop(3))




# Your code goes below here

8 Answers

olegovich7
olegovich7
6,605 Points
messy_list = ["a", 2, 3, 1, False, [1, 2, 3]]
messy_list.insert(0, messy_list.pop(3))
messy_list.remove("a")
del messy_list[3]
del messy_list[3]
Noah Harness
Noah Harness
1,383 Points

Yeah that isn't close to working dude

olegovich7
olegovich7
6,605 Points

Workpaces python shell:

>>>messy_list = ["a", 2, 3, 1, False, [1, 2, 3]]
>>>messy_list.insert(0, messy_list.pop(3))
>>>messy_list.remove("a")
>>>del messy_list[3]
>>>del messy_list[3]
>>>print(messy_list)
[1, 2, 3]

Code challenge:

Congrats, you've completed the challenge!
messy_list = ["a", 2, 3, 1, False, [1, 2, 3]]

# Your code goes below here

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

i = 0
for letter in messy_list:
    print(messy_list[i])
    if isinstance(messy_list[i],( bool, str, list, tuple)):
        messy_list.pop(i)
    i += 1
messy_list.pop(-1)

## I tried to check item for type == list. but it doesnt work. i also tried use try catch item / item. hope this help
messy_list.remove("a")
messy_list.remove(False)
messy_list.remove([1, 2, 3])
Noah Harness
Noah Harness
1,383 Points

nevermind I spelled something wrong thanks man I really appreciate it haha

Billy Feighery
Billy Feighery
9,866 Points
messy_list = ["a", 2, 3, 1, False, [1, 2, 3]]

# Your code goes below here

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


clean_list = []

for i in messy_list:
    if (isinstance(i, int)) and not (isinstance(i, bool)):
        clean_list.append(i)
    print(clean_list)
messy_list = clean_list

It's not overly pretty, but it works.

Noah Harness
Noah Harness
1,383 Points

Huh its not working for me. Its saying that part one is no longer working

import numbers

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

Your code goes below here

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

messy_list = [x for x in messy_list if isinstance(x, numbers.Number)] del messy_list[3] print(messy_list)

Christopher Reynolds
Christopher Reynolds
2,817 Points

as question:

why doesn't this work

messy_list = ["a", 2, 3, 1, False, [1, 2, 3]] messy_list.insert(0, messy_list.pop(3)) messy_list.remove(False) messy_list.remove("a") messy_list.remove([1, 2, 3])