Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
Elgene Ee3,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
Alex KoumparosPython Development Techdegree Student 36,862 Points
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 Bartulis2,422 Points
comma = ", "
for group in musical_groups:
Dave StSomeWhere19,822 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='')