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

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

I don't understand this, it confused me so much

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

Breaking it down:

# the last 6 elements of the list rainbow
rainbow[-6:]
# "".join() creates a string using these elements. 
# "" means the separation between elements is the empty string, that is, no space
"".join(rainbow[-6:])
# wrapping the expression in [ ] creates a list with the joined string as the only item
["".join(rainbow[-6:])]

Creating a string from a list of items can seem confusing. A built-in method on strings is the .join(some_list) method. It says "use this string as a connecting segment to create a new string using each of the items in some_list"

An example:

>>> a = ['1', '2', '3', '4', '5', '6']
>>> "---".join(a)
'1---2---3---4---5---6'
>>> "-".join(a)
'1-2-3-4-5-6'
>>> "".join(a)
'123456'

Post back if you have any more questions. Good luck!!

Thanks so much, I didn't realise what u needed to join with came before .join, well now I understand this but a lot of the stuff in my stage is actually really confusing me, but I don't want to start the course all the way from the beginning, I don't know what to do

Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

Sometimes repeating content can help to reinforce what you know and help learn the concepts that need a better understanding. Feel free to post more questions to the community forum if you get stuck again.

I guess its the quotation marks that really throw me

Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

Answer expanded above to include more .join() examples.