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 challenge: REMOVING ITEMS FROM LIST

So I completed the challenge by just typing into the code to delete the exact items I wanted deleted, but I wasn't able to do it in a loop. I would appreciate some clarity and possibly the code to further understand. For those who don't know the challenge it is taking everything out of the list except the numbers in the first list. messy_list = ["a", 2, 3, 1, False, [1, 2, 3]]. Thank you in advanced for the help

1 Answer

John Lack-Wilson
John Lack-Wilson
8,181 Points

There are many approaches to take here, but the one that I took to pass this challenge was the following:

  • Create a new empty list (outside of the loop that you will need)
  • Loop over the elements in the messy_list
  • Check to see if each element is an integer or not, we can do this using type()
  • If it is an integer, then append it to the new empty list
  • After the loop, we can assign messy_list to the new list (so that the challenge will pass successfully)