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 trialkeith rezendes
4,459 Pointsmulti-dimensional list using join
how to flatten a 2D list using join method?
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 i in range(len(musical_groups)):
for j in range(len(musical_groups[i])):
print(musical_groups[i][j] + ',')
2 Answers
keith rezendes
4,459 PointsAwesome! Thank you.
Cooper Runstein
11,850 PointsWhat you're attempting may work in a real world situation, but not for the way this practice problem is set up. Also notice that you're printing only a comma, not a comma and a space. Using join is the more pythonic practice, shown below.
Part 1
for group in musical_groups: #for each group in musical_groups
print(', '.join(group)) #print a string created by joining each element in the group
Part 2
for group in musical_groups:
if len(group) == 3: #if and only if the group has 3 members (the list is length 3)
print(', '.join(group)) #print the joined string