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 trialRandall Varasteh
Python Development Techdegree Student 2,814 Points.join() help
On the challenge: Here is a multi-dimensional list of musical groups. The first dimension is group, the second is group members. Can you loop through each group and output the members joined together with a ", " comma space as a separator, please?
I'm confused about how the join function works in loops, and have tried other variations of my code attached, but I keep receiving the error: File "", line 13, in TypeError: sequence item 0: expected str instance, list found
I went back and re-watched both videos on multi-dimensional lists and the join function, as well as googled the function. I'm not sure what I'm not understanding. Please help, thanks.
musical_groups = [
["Ad Rock", "MCA", "Mike D."],
["John Lennon", "Paul McCartney", "Ringo Starr", "George Harrison"],
["Salt", "Peppa", "Spinderella"],
["Rivers Cuomo", "Patrick Wilson", "Brian Bell", "Scott Shriner"],
["Chuck D.", "Flavor Flav", "Professor Griff", "Khari Winn", "DJ Lord"],
["Axl Rose", "Slash", "Duff McKagan", "Steven Adler"],
["Run", "DMC", "Jam Master Jay"],
]
# Your code here
for band in musical_groups:
seperator = ", "
bands = seperator.join(musical_groups)
print(bands)
1 Answer
Jassim Alhatem
20,883 PointsHi Randall Varasteh,
You should take out the musical_groups and replace it with band. You got everything right, you just messed up with the iterable.
Your code should be:
musical_groups = [
["Ad Rock", "MCA", "Mike D."],
["John Lennon", "Paul McCartney", "Ringo Starr", "George Harrison"],
["Salt", "Peppa", "Spinderella"],
["Rivers Cuomo", "Patrick Wilson", "Brian Bell", "Scott Shriner"],
["Chuck D.", "Flavor Flav", "Professor Griff", "Khari Winn", "DJ Lord"],
["Axl Rose", "Slash", "Duff McKagan", "Steven Adler"],
["Run", "DMC", "Jam Master Jay"],
]
# Your code here
for band in musical_groups:
seperator = ", "
bands = seperator.join(band)
print(bands)
Check out GeeksforGeeks. They give a very good explanation of the .join() function.