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 Basics (Retired) Shopping List Lists and Strings

Replacing an item in a list

The question is asking me to replace "Treehouse" in greeting_list with the first item in name_list. I got the right answer by reading a forum question but I don't understand it.

What does the -1 part mean? The video doesn't actually go over that. It only talks about .append, which would not replace anything, only add to the end of the list from what I understand. Thanks!

full_name = "Ben Muresan"
name_list = full_name.split (" ")

hi = "Hi, I'm Treehouse"
greeting_list = hi.split (" ")

greeting_list[len(greeting_list) - 1] = name_list [0]

1 Answer

Thomas Kirk
Thomas Kirk
5,246 Points


The code is taking the length of greeting_list, which is 3 [Hi, I'm, Treehouse], and then subtracting 1 from it to get the index number of the last item in the list (i.e. the index number of Treehouse). Because index numbers start at 0, a list of 3 items will be indexed from [0] then [1] then [2].

So the code finds the greeting_list index number that is one less than the number of items in the list, or 3 minus 1.

I hope that makes sense.

I believe the code would pass if it simply read:

greeting_list[2] = name_list [0]