Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Collections (Retired) Lists Redux Manipulating Lists

Bob Amand
Bob Amand
2,639 Points

stuck on lists.py

Task 1 clears. I have been struggling with Task 2. For some reason when I make a mistake in Task 2 the system flags Task 1. Seem to be stuck in my own perpetual loop. I have attempted at least 15 alternative solutions yet no closer to completing. No longer learning.

lists.py
the_list = ["a", 2, 3, 1, False, [1, 2, 3]]
# Your code goes below here
the_list.insert(0, the_list.pop(3))

for item in the_list:
  if item != range(1,3):
    del the_list[item]

3 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Bob;

Welcome to Treehouse!

I can fully relate to the frustration of being stuck on some of these challenges, but you are doing great and asking questions is key!

Let's take a look at this task in particular.

Task 2

Use .remove() and/or del to remove the string, boolean, and list members of the_list.

Here is our list after we have completed Task 1

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

If you remember from the course videos we can remove things explicitly with remove() like:

the_list.remove("some string value")

or we can delete specific index values with del using the syntax:

del the_list[3]

which would remove the item at index 3.

I hope that gets you pointed in the right direction but please post back if you are still stuck or have further questions.

Happy coding,
Ken

Bob Amand
Bob Amand
2,639 Points

Excellent. I think I now understand the solution for Task 2. BUT, there must be something wrong with my Task 1 answer because it claims it longer passes once I put in Task 2! Here is my code for Task 1: -to move 1 from current location in one step. First it is accepted. Add Task 2 and it is not! the_list.insert(0, the_list.pop(3))

Ken Alger
Ken Alger
Treehouse Teacher

Bob;

Can you post the current code you are using? Sometimes the challenge checker is a bit, um, picky, yeah, let's go with picky.

Ken

Bob Amand
Bob Amand
2,639 Points

Ken, second try: Task 1 code that passes:

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

Task 1 doesn't pass when I attempt Task 2!

Thanks, Bob

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Bob;

Let's look at Task 2 this way...

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

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

# Your code goes below here

Keep in mind that if you are referencing items in Task 2 using index numbers, the code from Task 1 will change the index values of the_list for use in Task 2.

Ken

Bob Amand
Bob Amand
2,639 Points

I moved my Task 1 code above the "# your code below..." (not sure if it should matter but seemed to!). I understand the index shift and used: del my_list[1, 4, 5] did not pass. del my_list[1] del my_list[4] del my_list[5] did not pass with an "index out of range" now trying a combination of remove/del to determine if remove only for text, etc. is the issue.

Bob Amand
Bob Amand
2,639 Points

Ken, I finally convinced the system to accept my answer. I used the_list.remove(individual values).

I had tried every permutation; del the_list[5] kept giving me an index out of range error. Thank you for the help. I appreciate validating my Task 1 answer and the clues for Task 2.
Regards, Bob

Ken Alger
Ken Alger
Treehouse Teacher

Bob;

Way to stick with it!

Happy coding,
Ken

Bob Amand
Bob Amand
2,639 Points

Ken, Here is the code for Task 1:

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

a second try:

my_list = the_list my_list.insert(0, my_list.pop(3))

Thanks, Bob

Ken Alger
Ken Alger
Treehouse Teacher

Bob;

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

That works. Task 2 is built upon that so don't delete that line, just remove() or del items with code after that line.

Ken