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 (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 67,989 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 67,989 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 67,989 Points

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