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 trialKody Dibble
2,554 PointsPython Challenge .extend
Not sure how to use the .extend method correctly.
the_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[1:7]
the_list.extend(range(0, 21))
2 Answers
Jason Anello
Courses Plus Student 94,610 PointsHi Kody,
We should first back up to task 2 because you've deleted more items than you should have and this will affect the answer for task 3.
After removing the 'a' the list would look like [1, 2, 3, False, [1, 2, 3]]
At this point you only need to remove the boolean False and the list that comes after it.
With your del statement you're removing a slice of the list that begins at index 1 and goes past the end of the list(last valid index is 4).
All this leaves you with is [1]
and you needed [1, 2, 3]
It still passed the challenge but it shouldn't have been allowed.
So you need to fix the slice so that it starts at the index where the False
is and goes to the end.
At this point you should have the list [1, 2, 3]
when you start task 3.
Task 3 wants a list of all the numbers from 1 to 20. You already have 1, 2, 3 so you simply need to extend it by the numbers that are missing.
So you'll need to adjust the starting number on your range function in this code: the_list.extend(range(0, 21))
Iain Simmons
Treehouse Moderator 32,305 PointsThe last challenge task asks you to make the list contain the numbers 1 through 20. It should already have 1, 2, and 3.
As Xing Hui Lu points out, extend
will concatenate another iterable on to the end of the one you call it on.
So you need to extend
the the_list
iterable with another one containing the numbers 4 through 20. You can use the range
method, but you'll need to adjust the parameters to get the numbers you need.
Richard Lu
20,185 PointsRichard Lu
20,185 Pointsextend(...) L.extend(iterable) -- extend list by appending elements from the iterable
That's what it shows when you do a help(list.extend). It basically means you can concatenate another iterable object (i.e. string, list) onto the list. If I'm not wrong you could do this:
the_list.extend("myotheriterableobject")