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!

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

Michael Sothan
Michael Sothan
8,048 Points

How to calculate length of each row in multi-dimensional list

I need to print out all groups and their members if the members = 3 Not sure if I need to use the lens() function on 'musical_groups' or on 'group'. Or perhaps there is a better way than using lens ?

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 = 1
if lens(musical_groups) == 3:
    for members in musical_groups:
        print(", ".join(members))
group += 1
Michael Sothan
Michael Sothan
8,048 Points

Figured out the solution. The biggest problem was typing "lens()" instead of len(). However I'm a bit confused why it worked.

group = 1
for member in musical_groups:
    count = len(member)
    if count == 3:
        print(", ".join(member))
group += 1

To me it seems member refers to each individual element in the list and not each row. So len(member) would be the len of each string and not the # of elements in a row. To get the length of the row I thought you'd have to do len(group). I also thought len(musical_groups) could work understanding that because of the loop you are iterating the len function on each row individually and not the whole list.

But I guess member in my code above actually refers to each row of the list?

1 Answer

Daniel Turato
seal-mask
PLUS
.a{fill-rule:evenodd;}techdegree seal-36
Daniel Turato
Java Web Development Techdegree Graduate 30,124 Points

So firstly you need to loop through each group in which you use a regular for loop for this. Then at each iteration, you need to check if that particular group has 3 members in which you can use the len() method. So you had to the right idea, just in a different order. This is what I got:

for group in musical_groups:
    if len(group) == 3:
        print(", ".join(group))
Michael Sothan
Michael Sothan
8,048 Points

Sorry, just saw you posted this! Didn't receive an email for some reason. Thanks Daniel.

So, the group in for group in musical_groups actually refers to each row of the 2d list?

I was thinking it referred to each element in the list. Hence len(group) would calculate each string length and not the # of elements.

Daniel Turato
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Daniel Turato
Java Web Development Techdegree Graduate 30,124 Points

In musical_groups, each element is a list so when looping through its elements you will retrieve a list at each iteration. Now, at each iteration you have to check how many members (elements) are in that list. You can do this in two ways, using the len() method which is the easiest way or again create another inner for loop to count each element in that list. Hopefully this clears up any problems you had with this challenge.