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

James Cavanagh
James Cavanagh
1,747 Points

Unable to use "len()" inside a "for loop" for a multidimensional list to get the number of objects in second list lvl

I am trying to create a rule to only print the items in the "musical_groups" list that contain 3 objects themselves.

I have set up a "for loop" to run through each item in the "musical_groups" list. I have then tried to create an "if" rule to identify which item in the list has 3 objects.

I thought this would work using the "len(group)" == 3. But it doesn't seem to want to cycle through the items in the list.

Thanks in advance!

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"],
]
# Your code here
group_number = 0
for group in musical_groups:
    if len(group) == 3:
        to_line = ", ".join(musical_groups[group_number])
        print(to_line)
        group_number += 1

2 Answers

Louise St. Germain
Louise St. Germain
19,424 Points

Hi James,

I agree with what Kris said - although you can make your code even simpler.

Right now you are manually tracking group number, which you are using so that you have an index into musical_groups as the for loop cycles through it.

However, Python is already keeping track of that. The variable group in your code gets updated to the next item in the musical_groups list with every iteration, so it's the same as musical_groups[group_number] (where group_number is an index). So you can replace the line:

# replace:
to_line = ", ".join(musical_groups[group_number])
# with:
to_line = ", ".join(group)

Once you do that substitution, you'll see that you don't need the group_number index at all anymore, so you can scrap it altogether. Your updated code would look like:

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

Hope this helps!

James Cavanagh
James Cavanagh
1,747 Points

Thank you so much for your help! :)

Thank you, this helped me as well

Your group_number is only incrementing if the if statement evaluates to true. Outdent that line and you should be good:

group_number = 0
for group in musical_groups:
    if len(group) == 3:
        to_line = ", ".join(musical_groups[group_number])
        print(to_line)
    group_number += 1
James Cavanagh
James Cavanagh
1,747 Points

Thank you so much for your help :)