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 trialDavid Kan
4,058 PointsPython Collection Challenge Task 2 out of 3
Based on the instructions, I'm tasked to use the .remove() and/or del to remove the string, boolean, and list members of the_list. I'm not sure what I'm doing wrong I keep getting the "Oops! It looks like Task 1 is no longer passing". Am I not reading the question correctly? Because I'm removing "a", False, and [1, 2, 3] using both remove() and del.
the_list = ["a", 2, 3, 1, False, [1, 2, 3]]
# Your code goes below here
the_list.remove("a")
del the_list[3]
the_list.remove([1, 2, 3])
the_list
The result is [2, 3, 1]
Looking for guidance.
8 Answers
Jason Anello
Courses Plus Student 94,610 PointsHi David,
There isn't anything wrong with your del
and remove
statements but it looks like you removed the code from task 1.
You usually need to keep your previous code in place when going to the next task.
The result you should have at this point is [1, 2, 3]
because in task 1 you move the 1 to the front and in task 2 you remove everything that's not a number.
ellie adam
26,377 Pointsthe_list = ["a", 2, 3, 1, False, [1, 2, 3]]
# Your code goes below here
the_list.pop(3)
the_list.insert(0, 1)
the_list.remove("a")
del the_list[3]
the_list.remove([1, 2, 3])
Aaron Jorgensen
PHP Development Techdegree Student 1,124 Pointsthis was my answer to the problem, I'm sure it could be cleaner:
the_list = ["a", 2, 3, 1, False, [1, 2, 3]]
# Your code goes below here
the_list.insert(0, the_list.pop(the_list.index(1)))
the_list.remove("a")
del the_list[3]
the_list.remove([1, 2, 3])
Tony McCabe
4,889 PointsEllie Adam has the answer... These people in their questions. it would take a genius and serious algorithms to figure some of their problems.
Tim Forman
8,486 PointsThis is my answer for steps 1 & 2. Step 1 can be done with one line because .insert will accept two arguments.
the_list.insert(0, the_list.pop(3)) the_list.remove("a") del the_list[3] the_list.remove([1, 2, 3])
Richard Hanberry
762 Pointsthe_list = ["a", 2, 3, 1, False, [1, 2, 3]]
Your code goes below here
the_list.insert(0, the_list.pop(3))
the_list.remove("a")
del the_list[3]
the_list.remove([1, 2, 3])
It worked after I figured out that del requires square brackets instead of parentheses :)
Andrew Merrick
20,151 PointsIt's not a best practice but you can also repeat the del the_list[3]
twice:
the_list = ["a", 2, 3, 1, False, [1, 2, 3]]
# Your code goes below here
the_list.insert(0, the_list.pop(3))
the_list.remove("a")
del the_list[3]
del the_list[3]
Julia Utenkova
12,024 PointsHi everybody!
I have come up with a slightly different solution:
the_list = ["a", 2, 3, 1, False, [1, 2, 3]]
the_list.insert(0, the_list.pop(3))
the_new_list = []
for item in the_list:
if type(item) == int:
the_new_list.append(item)
print("Final List: ", the_new_list)
I know, this is not a part of a task, but it does the same functionality :)
Cheers!