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

Daniel Lounsbury
Daniel Lounsbury
1,744 Points

I am having trouble joining and printing 1 dimension of a list separated by comma's

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?

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"],

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"],

3 Answers

Daniel,
Your for loop is ok, but use of join and print method are incorrect. See solution below:

for members in musical_groups:
    print(", ".join(members))

The join method takes a list and outputs a string of the list elements joined by the preceding string it is called on (in this case ", "). Print just takes a string, it does not perform the joining operation your code implies. Hope this helps.

my code was in similar line ,i am getting output in python shell for member in musical_groups: if len(member)==3: print(", ".join(member))

but not sure whats the issue when we submit the answer treehouse task

Daniel Lounsbury
Daniel Lounsbury
1,744 Points

I think mine ended up looking like this,

for member in musical_groups: if(len(member) == 3): print(", ".join(member))

Daniel,

Do you have any code you have tried that we could look at?

Daniel Lounsbury
Daniel Lounsbury
1,744 Points

currently I have the following but now I am lost :( It says I have 2 errors

for members in musical_groups: members = (musical_groups).join

print (members ", ")