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 Collections (2016, retired 2019) Slices Deleting Or Replacing Slices

@ 3:30 I don't get the "" when joining the code back together

At around 3:30 the following code is displayed when trying to fix the ['v', 'i', 'o', 'l', 'e', 't'] the list called rainbow and there are other stuff in the list, but all you need to know is v is the index -6, i is the index -5, etc

He does

rainbow[-6:] = ["".join(rainbow[-6:])]

I don't get why he is joining "" with rainbow[-6:]

3 Answers

Ronald Lira
Ronald Lira
12,217 Points

To understand statements like this I like to go to from the most inner part to the outer part. Let's start by considering

rainbow = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'v', 'i', 'o', 'l', 'e', 't']

Then, the slice rainbow[-6:] will return the last 6 items on the list. We will effectively have:

>>> rainbow[-6:]
>>> ['v', 'i', 'o', 'l', 'e', 't']

Next, we have the join method used at "".join(rainbow[-6:]). The official documentation shows str.join(iterable). This means the join method is called on a string str and we must also pass an iterable object. The string specified is used between each of items that belong to the iterable object. By passing an empty string "" we are saying: we don't want anything between the items. Also important is to keep in mind that the return will be a new string.

>>> "".join(rainbow[-6:])
>>> "violet"

Finally, the brackets []. Remember that in Python you can create a list in one of the following ways:

>>> x = list()
>>> x
[]

or

>>> x = []
>>> x
[]

Here we use the second method, but instead of creating an empty list, we are passing a single element to the list as its very first element.

>>> x = ["violet"]
>>> x
['violet']

The very end is just a substitution of the last 6 elements in the rainbow list for this single element.

Hope this helps.

Good luck

Chandelor Simon
Chandelor Simon
2,242 Points

I know this was a year ago - but Ronald Lira: that's a beautifully constructed explanation; though I wasn't tripped up by this in the video I still read the whole thing to further solidify my understanding and you laid it out perfectly here. Way to go, bro.

Dear Marie,

might you check the command seperate.

rainbow = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'v', 'i', 'o', 'l', 'e', 't']
print(rainbow[-6:])
['v', 'i', 'o', 'l', 'e', 't']
print("".join(rainbow[-6:]))
violet

rainbow[-6:] are all six last item in the list. and he "overwrite" all this a List with one string (join of the last 6) the [] are importend around the command.

Try

rainbow[-8:] = ["".join(rainbow[-6:])]
['red', 'orange', 'yellow', 'green', 'violet']

You just "overwrite" than more because of the -8

I hope a bit clearer. Greetings Julian

The empty string "" is what will be joining the elements. If he replaced "" with "." the result would have been v.i.o.l.e.t.