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

Elgene Ee
Elgene Ee
3,554 Points

Musical Groups help!

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 am unsure of what does the first dimension here mean, can anyone help me? If so which loop (for/while) should I use right here?

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

5 Answers

Alex Koumparos
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi Elgene,

Here is the video for multi-dimensional lists. In it you will see which is considered the first dimension.

You can make either kind of loop work here but generally speaking, for loops are better for situations where you know (or at least the loop knows) at the time of entering into the loop how many iterations you will have (i.e., you know the length of the thing you are iterating over at runtime). While loops are better for situations where the number of iterations is indefinite at the time the loop is entered into (e.g., a menu that takes user input - the user decides as he or she is using the menu when the menu will end).

Elanas Bartulis
Elanas Bartulis
2,422 Points

Answer :

comma = ", "

for group in musical_groups:

Dave StSomeWhere
Dave StSomeWhere
19,870 Points


musical_groups is multi-dimensional because it is a list of lists. The first dimension (group) is defined by the outermost brackets ("[ ]"). Then each line surrounded by brackets and comma separated, is the second dimension group members, like ["Ad Rock", "MCA", "Mike D."],

So, the challenge to loop through the groups (first-dimension/list) and output the join action in the members (second-dimension/list). I would use a for loop to loop through a list.

Does that help you get rolling?

This should work.

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

I kept getting "not what I was expecting" for a result. Then, after re-reading the instructions carefully, I realized he was asking for "comma space" and I was just using a comma. This is what I got to work for me:

for group in musical_groups: print(", ".join(group),end='')