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 trialDylan Cowling
Python Web Development Techdegree Student 2,590 PointsHit 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!
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
Alexander Davison
65,469 PointsWhen 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 offormat
.
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.)
Adam N
70,280 PointsYeah, overcomplicating. Use python's join function to get this done.
Not sure what the counter variable there is for, either.
Dylan Cowling
Python Web Development Techdegree Student 2,590 PointsAlright, thank you. I will have another crack at it :)
David Valverde
3,198 Pointsfor items in musical_groups: print(", ".join(items))
Sidharth Kaushik
Courses Plus Student 1,329 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 members in musical_groups: print(", ".join(members))
Dylan Cowling
Python Web Development Techdegree Student 2,590 PointsDylan Cowling
Python Web Development Techdegree Student 2,590 PointsThank you, I appreciate you making me think too! Can't learn anything by just copying others! :D