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 trialFlore W
4,744 PointsCleaning messy list with a function - everything works except for the last step, help please!
Hi all, I tried to create a function that would clean any messy lists for non integers. My function works until the last item in the loop. I hope you can see my code in lists.py (please tell me otherwise how I can attach my code to this question), and here below is the result from the console at each step:
[1, 'a', 2, 3, False, [1,2,3], [1,3,2], True] #that's my print(messy_list)
['a', False, [1,2,3], [1,3,2], True] #that's my blist, or the list of non-integers to remove
[1, 2, 3,False, [1,2,3], [1,3,2], True] #first iteration of my for loop -> removed the 'a', hurray
[1, 2, 3, [1,2,3], [1,3,2], True] #second iteration of my for loop -> removed the False, hurray
[1, 2, 3, [1,3,2], True] #third iteration of my for loop -> removed the [1,2,3] list, hurray
[1, 2, 3, True] #fourth iteration of my for loop -> removed the True, hurray
[2, 3, True] #fifth iteration of my for loop and last -> WHAT HAPPENED? Why did it remove my 1 and not the True?
messy_list = ["a", 2, 3, 1, False, [1, 2, 3]]
# Your code goes below here
messy_list.insert(0,messy_list.pop(3))
def isboolean(item):
if type(item) in (float,int):
return item
else:
None
def remove_notint(alist):
blist = []
for item in alist:
try:
int(isboolean(item))
except TypeError:
blist.append(item)
for item in blist:
try:
alist.remove(item)
print(alist)
except ValueError:
pass
return alist
print(messy_list)
remove_notint(messy_list)
2 Answers
Steven Parker
231,275 PointsThis challenge doesn't require writing any functions. Just as you passed task 1 with a single line, the challenge can be completed with a few additional lines of code.
The instructions suggest you remove each item with either a "del" statement or the ".remove()" method.
Flore W
4,744 PointsOh wow, that's such a short and elegant way to put it ! thanks Steven, it is particularly good to know that remove treats True and False as 1 and 0.
Steven Parker
231,275 PointsLove those list comprehensions (there's a workshop on them)!
Happy coding!
Flore W
4,744 PointsFlore W
4,744 PointsHi Steven, thanks, yes I know that, but I wanted to practice a bit more and write a function that would make any messy list clean. Actually I passed the challenge with my code above but it is wrong on the console... so I'd really want to know what's wrong with it. Any help is welcome!
Steven Parker
231,275 PointsSteven Parker
231,275 PointsOk, well, "remove" treats "True" the same as "1" , and "False" the same as "0", just as ordinary comparisons do:
So when you `alist.remove(True)", it finds the 1 first and removes that.
You could make your cleaner handle this by just returning the items that are of the desired type: