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

David Sanchez
David Sanchez
623 Points

Multidimensional Lists - Stuck

I'm stuck on the second portion of the Coding Challenge for multidimenstinoal lists. Could someone please assist?

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 group_members in musical_groups:
    print(", " .join(group_members))

Hey David: "Len" function will help you to find the length of a iterable(list, strings, tuple and more). Remember in case of multidimensional we have a list inside of a list. Incase of your doubt it is two dimensional list. So when you write: for group_members in musical_groups: print(", " .join(group_members))

Note that join method always takes in an iterable and you have done it correct. It is basically you are iterating over other sub lists present under the main list i.e musical_groups. Remember that the variable group_members that you created is the sub list that will contain the member's name in each of the group. now we can also loop over the group_member as they are also iterable.

Now coming to your doubt: len function also takes in an iterable. Thus, you can put you code under an if statement:

for group_members in musical_groups: if len(group_members) == 3: print(", " .join(group_members))

** A BIT OF WARNING PLEASE DON'T COPY THE CODE. TRY TO UNDERSTAND. IF YOU STILL HAVE DOUBT ASK UNTIL YOU MASTER IT. **

Have a good time Learning

3 Answers

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

Try this code. Remember the code challenge is syntactically conscious so it's looking for a particular way that the code is formatted.

So what we're doing is writing a condition statement that checks if the particular iteration of the loop has a list that is exactly 3 items long... which is the trio of names we want. So that's why we pass group_members` because this is called a working variable.

We then print the remaining filtered parts of the loop.

for group_members in musical_groups:
    if(len( group_members ) == 3)
       print(", " .join(group_members))
David Sanchez
David Sanchez
623 Points

Thank you. So why wouldn't the code look like what i've shown below with the number 3 in braces?

for group_members in musical_groups: if(len(group_members) == [3] print(", " .join(group_members))

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

You need to find a way to print only run the code when the length of the array inside the musical_groups array is 3.

There's a method you can use to do that called len(). write an if statement that checks for that condition and passing it inside your loop.

David Sanchez
David Sanchez
623 Points

I tried using the len function as you suggested, but I'm still getting errors.

I'm having issues also. I used your code Jonathan Grieve but get a syntaxerror: It points the the 3 or somewhere around there being the issue:

for band in musical_groups: seperator = ", " bands = seperator.join(band) print(bands)

for group_members in musical_groups: if(len(group_members) == 3) print(", " .join(group_members))

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points
for group_members in musical_groups:
    if( len( group_members ) == 3):
        print(", " .join(group_members))

Using the code I originally wrote, add another colon to the if statement. I must have forgotten to do that in my answer. :)