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 PointsPython's lists last challenge.
Hi everybody! I'm not able to find the solution to the last problem. here's what I tried:
for group in musical_groups :
print(", ".join([member for member in group if len(member) == 3]))
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 :
print(", ".join([member for member in group if len(member) == 3]))
4 Answers
Steven Parker
231,269 PointsYou won't need anything this fancy. Plus, this will print out empty lists when the size is other than 3.
The simple approach is to just put an "if" before the "print" that was already there from task 1, so that it only runs when the list has 3 members.
Ave Nurme
20,907 PointsHey Alberto
I think you need to find trios that is groups which consist of 3 members so I think that len(group)
might be useful. I solved the challenge without list comprehension. At first I iterated through the groups in musical_groups
and checked the length of each group. If the length was 3 (as in 3 members) I joined them into a string separated by a comma.
Alberto Serafini
1,211 PointsThank you Ave and thank you Steven! Yes, I think I went too far with list comprehension and now I'm a bit lost. I'll try to follow the hints you gave me and then I'll let you know how it goes. thanks!!
Alberto Serafini
1,211 PointsThank you so much Steven! You made my day! Here's what i did:
for group in musical_groups:
if len(group) == 3:
print(", ".join(group))
Do you have any advice for me to do a better job of looking for the most simple solution, instead of over-complicating things? what should I work on? Thank you so much again!
Steven Parker
231,269 PointsThree suggestions: practice, practice, and practice.