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 trialnikel Hayo
1,943 Pointshow can i get rid of the first comma?
I am trying to solve this quiz with the least amount of code, but I could not figure out to make the loop without creating first comma. I do not want to delete first comma after output,as it will make the code complicated . Is there any way to re-write this code as simple as possible, directly giving the requested output?
this is the result of my code:
, 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
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"],
]
for group in musical_groups:
joined_member=""
for member in group:
joined_member=joined_member+ ", " + member
print(joined_member)
2 Answers
Steven Parker
231,275 PointsYou don't need an inner loop, just use the "join" method on each group. It only places delimiters between the items joined:
print(", ".join(group))
nikel Hayo
1,943 PointsThanks Steven it is amazing how simple the solutions are at python . At which course was this join method explained? I somehow missed that.
Steven Parker
231,275 PointsIn the course you are taking now, the previous stage has a video titled Split and Join.