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 trial

Python Introducing Lists Build an Application Multidimensional Musical Groups

Daniel Lounsbury
Daniel Lounsbury
1,744 Points

How do I use the len function to print only lists that are 3 items long

How do I use the len function to print only lists that are 3 items long?

I am totally stuck on the second part of this task. I'm not even sure which video I need to rewatch to find the answer.

groups.py
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"],
]
for members in musical_groups:
    print(", ".join(members))

2 Answers

Cooper Runstein
Cooper Runstein
11,850 Points

You need a conditional, an if statement will do. Imagine this abstracted into rows:

my_data = [
row1,
row2,
row3,
....
]

Where each row has some items in it:

row1 = [item1, item2 ...]
row2 = [item5, ....]
....

You have the first part right:

for row in my_data:
    #do something

So what do you need to do for each row? You need to check the length, and if the length is 3, you need to print that row, which you know how to do:

if(len(row) == 3):
    print(row)

Now you just need to put it all together, let me know if you have any trouble doing so!

Daniel Lounsbury
Daniel Lounsbury
1,744 Points

Thank you for your help, I am just not understanding how to implement the examples you gave. Am I supposed to label each of the bands so that I can call a print function for specified ones or does the answer lay in using the Len function, in which case in what way should I label the "bands"?

Cooper Runstein
Cooper Runstein
11,850 Points

What is the exact language of the question?

Daniel Lounsbury
Daniel Lounsbury
1,744 Points

like this?

my_data = [ row1, row2, row3, row4, row5, row6, row,7 ]

row1 = "Ad Rock", "MCA", "Mike D." row2 = "John Lennon", "Paul McCartney", "Ringo Starr", "George Harrison" row3 = "Salt", "Peppa", "Spinderella" .....

It seems like I need to use the Len function on the "members" and if it ==3 then print but I can't seem to make it work