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

Dylan Cowling
seal-mask
.a{fill-rule:evenodd;}techdegree
Dylan Cowling
Python Web Development Techdegree Student 2,590 Points

Hit a mental block on this one, how do I output them all separated by commas and a space?

This is the objective:


Here is a multi-dimensional list of musical groups. The first dimension is a 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?


Am I overcomplicating things? I honestly have no notes on this kind of task so I'm a bit stumped! Thank you in advance!

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"],
]
group_number = 1
for group in musical_groups:
    print(", {}".format(group_number))
    group_number += 1

4 Answers

When I run your program, I get this in response:

, 1
, 2
, 3
, 4
, 5
, 6
, 7

However, the challenge expects you to get this in response:

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
  • You don't need to use the group_number variable, and
  • You need to use join instead of format.

Something like this, maybe:

for ...:
    print(', '.join(...))

(I'm intentionally leaving out parts of the code so that you won't just copy-and-paste.)

Yeah, overcomplicating. Use python's join function to get this done.

Not sure what the counter variable there is for, either.

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

Sidharth Kaushik
PLUS
Sidharth Kaushik
Courses Plus Student 1,329 Points

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 members in musical_groups: print(", ".join(members))