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 trialmm78
5,190 PointsIntroducing Lists - Challenge Task 2 of 2
Hello -
Although I can print the number of members within musical_groups I'm unable to return groups with three members.
My original thought was creating a function that populates an empty list using the append method, but thus far my function is inoperable.
Any help is greatly appreciated.
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:
members = ", ".join(group)
print(members)
print(len(group))
3 Answers
Blake Kazmierzak
2,491 PointsFrom what I understand, you are trying to return groups that only have exactly 3 members. So, inside your for loop, you should include an 'if' condition that restricts the results to items containing only 3 members.
Example:
for group in musical_groups:
if len(group) == 3: # checks to see if the length of the group is equal to 3 members
print(group) # prints only those items that have a length of 3
Steven Parker
231,236 PointsYou won't need to "print" the size of the group. But you'll need to test it to see if the group has three members, and print the group members (or not) based on that test.
An "if" statement would be good for this.
mm78
5,190 PointsThank you!
Script is working perfectly now.
Steven Parker
231,236 Pointsmm78 — Glad to help. You can mark a question solved by choosing a "best answer".
And happy coding!