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 trialMohammed Zvakavapano
1,746 PointsThe first dimension is group, the second is group members.
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"],
]
print(comma.join(group))
# Your code here
4 Answers
Michael Hulet
47,913 PointsThere's some scary terminology here, but it's not at hard as it sounds. In different terms, musical_groups
is just a list that contains a bunch of other lists. All those lists contain strings, which you need to join with a comma (,
).
You will need to write a for
loop here to get each list in musical_groups
, but then you can join each one with the join
function on str (which it looks like you've caught on to, though you'll need to call it on a literal string ","
instead of on the currently-undefined variable comma
).
For example, this does a similar thing, but adds together a bunch of numbers:
matrix = [
[1, 2, 3, 4],
[5, 6, 7, 8, 9],
[3, 2, 8, 5]
]
for row in matrix: # This grabs each list in the matrix array, one at a time, and assigns them to row
print(sum(row)) # This will add the numbers in each row together and prints them out
The above code will print something like this:
>>> 10
>>> 35
>>> 18
taurai sibanda
2,581 Pointsprint(", ".join(group))
Michael Stone
3,087 Pointsmusical_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 row in musical_groups:
print(", ".join(group))
My code isn't working. Can anyone help?
Michael Stone
3,087 PointsNevermind I figured it out.
for group in musical_groups:
group = ", ".join(group)
print(group)