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 trialAlberto Serafini
1,211 PointsHelp! Python, list course, last challenge.
Hi everybody , i can't solve the problem in the last challenge of the lists's course. I know there's a For, in , and a join method involved but i tried some stuff nothing works. Please help.
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 group in musical_groups:
Tim Oltman
7,730 PointsHi Alberto,
You got the first line right. Now what do you want to do with each group? Remember that .join is a method for strings. For example:
date = ["2020", "June"]
print("-".join(date)) # "2020-June"
3 Answers
sinan cengiz
24,052 PointsHi Alberto, You should loop over the group and in each iteration you should join a group then add in a new list I just put the working example down I hope it helps
Your code here
output =[]
for group in musical_groups:
s = ", "
s = s.join(group)
output.append(s)
print(output)
Alberto Serafini
1,211 PointsThank you very much Sinan and Tim you both were really helpful and I was able to solve the problem!! After I solved it, I did some more research and I was able to find another solution as well. Is anyone able to explain it to me? Here's the code:
For group in musical_groups:
print(", ".join([str(member) for member in group]))
sinan cengiz
24,052 PointsHi Alberto
Basically This is called list comprehension in python . Instead of using loop an putting in a list , you can just use this syntax in one line . I hope his youtube video can help to better understand.
Alberto Serafini
1,211 PointsThank you very much again Sinan, video very helpful!
sinan cengiz
24,052 Pointssinan cengiz
24,052 PointsHi Alberto, You should loop over the group and in each iteration you should join a group then add in a new list I just put the working example down I hope it helps
Your code here
output =[]
for group in musical_groups: s = ", " s = s.join(group) output.append(s)
print(output)