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 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]