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 Introducing Lists Build an Application Multidimensional Musical Groups

Curtis Thomas III
seal-mask
.a{fill-rule:evenodd;}techdegree
Curtis Thomas III
Python Web Development Techdegree Student 655 Points

Join function

I am confused on how to use the join function

groups.py
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
s = ", "
for name in musical_groups: 
    print(s.join(musical_groups))

1 Answer

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,720 Points

The join method on the string is a very interesting and useful method. The method returns a string representation of a list with the joining string part inserted between each element of the list.

beatles = ["John Lennon", "Paul McCartney", "Ringo Starr", "George Harrison"]
print( ", ".join(beatles))

Note the ", " goes in between each element of the list. Thus... the above code prints the following:

John Lennon, Paul McCartney, Ringo Starr, George Harrison

In the challenge we are given a list or lists. This tipped you off perfectly-- we are going to need a "for" loop to go through the elements of the musical_group variable. But! It is important we join the individual group, and not all the musical_groups as you did.

Your code will look something like this.

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
s = ", "
for group in musical_groups:
   print( s.join(group) ) # note, print the group, not the entire list of lists.

Your output will like something like this:

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