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 trialchikangsong
755 PointsI'm confusing for using .join method.. in LIST function.
I have a stupid question but, i want to truly the reason, please help.
for example..
sentence_list = ['A', 'B', 'C', 'D', 'E'] '_'.join(sentence_list) 'A B C D E' # it can be make a string..
But, when i use this method like below there is an error Why??
sentence_list.join() Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'list object has no attribute 'join'
What does the error mean? and The join method is not a list function?
Thank you
1 Answer
Richard Lu
20,185 PointsIf you type in help(list), you will not see the method for join. This is in fact a method of the str class. Type in help(str.join) and you'll see that the method takes any iterable object (i.e. list, tuple, string, etc.).
So for example, we could do
' * '.join(['a', 'b', 'c', 'd']) and - we'll get an output of 'a * b * c * d'
or
' * '.join('abcd') - which would give you the same thing
shibashis
8,989 Pointsshibashis
8,989 PointsYou cant join list, you can use join on strings. Try dir on both types of objects to see what is allowed.