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 trialAnthony Julian
Courses Plus Student 6,313 PointsHow do I only show the groups of 3 in a multidimensional array?
Struggling with Introducing Lists, final code challenge task 2. How do I only show the groups with 3 members? (i.e. ["Ad Rock", "MCA", "Mike D."], ["Salt", "Peppa", "Spinderella"], ["Run", "DMC", "Jam Master Jay"])
Here is the code I have so far:
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 members in musical_groups:
print(", ".join(members))
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 members in musical_groups:
print(", ".join(members))
1 Answer
Brandon White
Full Stack JavaScript Techdegree Graduate 35,771 PointsHi Anthony,
If I were attempting to show only the groups of three members joined as a string, I would test if the length of the current list were three, and I would only join and print the lists that evaluated to true on those tests.
Anthony Julian
Courses Plus Student 6,313 PointsAnthony Julian
Courses Plus Student 6,313 PointsThanks Brandon, however I ran an "if" statement and it didn't work. Please elaborate further? Thanks
Brandon White
Full Stack JavaScript Techdegree Graduate 35,771 PointsBrandon White
Full Stack JavaScript Techdegree Graduate 35,771 PointsNo problem.
The above is your code currently. Python loops through the musical _groups list and grabs every item (which is another/nested list) and prints it.
But if you donβt want it to print every list item/musical group, you could test:
This way during each iteration of the loop python says, are there only three items in this nested list (musical group)? If there are, it prints those members. If not, it skips the print function and starts on the next iteration of the loop.
Make sense now?
Anthony Julian
Courses Plus Student 6,313 PointsAnthony Julian
Courses Plus Student 6,313 PointsThanks so much Brandon. I actually had that method correct, but had len(musical_groups) instead of len(members). Makes much more sense now. Cheers