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 List creation/extension methods

Aditya Mehra
Aditya Mehra
5,970 Points

I want my_list to have 1 as the first item. Complete this method call: my_list._______(1) What is the method?

Which method is used?

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,064 Points

Yuck! The quiz is asking a horrible question. The answer expected by the quiz is append.

This is horrible since this answer only apples when my_list is an empty list: []

If my_list were empty, I would not need a method to add the value, I could simply assign it:

    my_list = [1]

The answer when my_list is not empty then answer would be:

    my_list.insert(0, 1) #<-- insert, at index 0, the value 1

UPDATE: Kenneth has updated the question to read "I want my_list to have 1 as the last item. Complete this method call: my_list._______(1) What is the method?". This makes SO much more sense now. Expected answer remains append.

Aditya Mehra
Aditya Mehra
5,970 Points

Got you.....Thanks Chris.

Hey Chris, wouldnt the .extend() funtion also allow us to plac the 1 at the end of the list?

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Jan M .extend() requires an iterable and ints aren't iterable, so, no, it'll cause a TypeError instead.

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Yep, that's me putting in "first" when I meant "last". Question has been updated. Thanks both of you!

Chris Freeman
Chris Freeman
Treehouse Moderator 68,064 Points

MUCH less horrible now, in fact it now makes perfect sense! Thanks for the update!