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

Frances Angulo
Frances Angulo
5,311 Points

Problems working with multi-dimensional groups

I'm really struggling with this one. I get the issue here is that these are lists within a list- but the best I can do is break down the list. I think I could work through this by creating an entire function like we saw in the Wishlist.py example- but that seems overkill. I think I'm realizing that I don't fully understand the role of loops - why can't I just return the list? Why does it need a loop?

Regardless, I don't know where the join goes. We saw this in the attendees example:

to_line = ", ".join(optional_invitees) print("To: " + to_line)

But I'm having trouble integrating this concept into the loop.

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

print("bands:") 
for groups in musical_groups: 
    print("* {}:".format(groups))

4 Answers

Julian Addison
Julian Addison
13,302 Points

in your for loop, think of each list within the list of lists as a group. In that case:

for group in musical_groups:
    print(group[0])

prints the first member of each group. you can nest further.

all_band_members = []
for group in musical_groups:
    for member in group:
        all_band_members.append(member)

this makes a new list and adds every band member in each group to it. think of what you can do at each level of the list you're targeting and how to target the information you need at that level.

What methods do we know of that can join the elements of a list into a string?

Frances Angulo
Frances Angulo
5,311 Points

Yikes- that got even worse:

all_members = []
for each_list in musical_groups:
    for member in each_list:
        all_members.append(member)
        ', '.join(member)
        print(all_members)

But then I gave this a whirl:

all_members = []
for each_list in musical_groups:
    for member in each_list:
        all_members.append(member)

join = ", ".join(all_members) 
Anupam Kumar
Anupam Kumar
3,795 Points

I am not sure, Where it went wrong can any look at these codes 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"], ] master_string=[] for lst in musical_groups: master_string.append(", ".join(lst))

Hey if you are still having problems this code works right here.

for member in musical_groups: member = ", ".join(member) print(member)