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

Joe Law
Joe Law
5,040 Points

HELP needed - Challenge: Manipulating Lists

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

Your code goes below here

num = the_list.pop(3)

the_list.insert(0,1)

the_list.remove('a')

the_list.remove(2)

the_list.remove(3)

the_list.remove(bool(False))

the_list.remove([1, 2, 3])

the_list.extend(range(2,21))


1) Why the range is (2,21) instead of (2,20)? 2) Is the any better method to remove items rather than the one i used?

2 Answers

hie Joe. firstly for challenge 1, in order to do that in one step you can try combining your two steps like this.

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

this line creates a new variable new_list that stores the updated list. we first use the .insert method, we want to insert our number at index 0 and what we want to insert is the 1 which we can obtain by using the pop like you did.

for challenge two we are told to remove the string, boolean, and list members of the_list. you did that quite well for the string and the list of numbers. you also didn't have to remove the 2 and 3, ist not stated in the question. you also didn't have to add the prefix bool because its already built in that is the language already knows that False is a bolean.

the_list.remove('a')

the_list.remove(False)

the_list.remove([1, 2, 3])

lastly we want to extend our list so that it contains only the numbers 1 through 20. NOTE our list already contains the numbers 1,2 and 3. now to add 4 to 20 we use

new_list = the_list.extend(range(4,21))

this we add the numbers 4 upto 21. hop this helps, don't hesitate to get back to me if it doesn't work.

Joe Law
Joe Law
5,040 Points

Thanks Dephine! Wooow, really thank you! :D

glad I could help

Andy Manning
Andy Manning
628 Points

Haha, I wasted so much time trying to do this in one in a loop! The simplest answer is always the best :)