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 (Retired) Lists Redux Manipulating Lists

explicit help with Code Challenge Manipulation of Lists, please

Please give me the full solution to this exercise. I have tried a for-loop, but Python changes the variable on the fly, which messes up the indices. So I tried a bypass by renaming all non-numbers, but this does not work either. I have tried for several hours and need to move on. Please give me the expected solution. Thank you.

for i in the_list: try: float(i) except TypeError or ValueError: i = "remove"

the_list.remove("remove")

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

The challenge can be solved using:

# Task 1 of 3
# Move the 1 to position 0. You can do this in one step with
# .pop() and .insert().

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

# pop the '1' (item at index three) and insert at position 0
the_list.insert(0, the_list.pop(3))

# Task 2 of 3
# Use '.remove()' and/or 'del' to remove the string, boolean,
# and list members of the_list.
the_list.remove("a")
the_list.remove(False)
the_list.remove([1, 2, 3])

# Task 3 of 3
# Now, make the_list contain only the numbers 1 through 20
# by using '.extend()'.
the_list.extend(range(4, 21))
Jordan Bester
Jordan Bester
4,752 Points

hey how is task 3 correct i'm not quite understanding the range function??

What I dont understand is how or why is 4,21 being used to represent the range of numbers between 1 and 20?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

Range(start, stop) goes from start to stop, including the start, and upto but not including the stop. So range(4, 21) produces 4 through 20.

Aaaah.... fair enough :-) I assumed the code should work more generally, and got in trouble detecting the type of each list member in a loop. If this is the accepted solution, then the task is really easy. So I have not missed anything important from the lessons. Thanks!