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

Chauncey Pelton
Chauncey Pelton
1,432 Points

How do I list names in the other groups?

I can only get the console to print out the first row of names over and over.

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 = 1
for groups in musical_groups:
    print('Group #{}: {}'.format(group_number, ', '.join(musical_groups[0])))
    group_number += 1

2 Answers

I would simplify your code a little, you don't need to format the string with group # and all that jazz.
As far as the re-printing, look at what you are doing in your for loop. Basically, for each group in that list of groups, you are printing the group at index [0].

Chauncey Pelton
Chauncey Pelton
1,432 Points

Thank you Kristen for the reply! It makes sense now about what you said. I was only getting that group at index [0]

I'm still not 100% clear on how I got it to work right, but I think my problem has been that I don't fully understand whether or not a variable is created while writing 'for x in y:'

I changed 'musical_groups[0]' to 'groups.' and that did the trick. Is it correct to think that 'groups' becomes a variable which holds each list in the larger 'musical_groups' list? Then with the 'for / in' statement the code will iterate over each of those lists?


Your code here

group_number = 1 for groups in musical_groups: if len(groups) == 3: print('Group #{}: {}'.format(group_number, ', '.join(groups))) group_number += 1

To your question, 'for x in y', will iterate over whatever iterable is inside of y. You can name x whatever you would like, but since y in your case is a list of lists, the iterable inside of the list y is a group of lists, which each will be called x. When you are actually writing the loop, think of what you want to happen to each list inside of that 'master' list. I hope that helps.