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

One list incorrect return. Why?

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"], ] def trios(musical_groups): trios_groups=musical_groups.copy() for trio in trios_groups: if len(trio) >3: trios_groups.remove(trio) return trios_groups

print('Musical Groups:') group_number = 1 for group in trios(musical_groups): group =", ".join(group) print('Group {}:{}'.format(group_number,group)) group_number +=1

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"],
]
def trios(musical_groups):
        trios_groups=musical_groups.copy()
        for trio in trios_groups:
                if len(trio) >3:
                        trios_groups.remove(trio)
        return trios_groups



print('Musical Groups:') 
group_number = 1
for group in trios(musical_groups):
        group =", ".join(group)
        print('Group {}:{}'.format(group_number,group))
        group_number +=1

I know there were easier ways to go about this, but I wanted to get 'remove' to work. My problem here is I needed to make two copies. By removing list from the array changes the index value of the lists causing list(s) in the array to get skipped. The code needs to loop through an array (or list) that is not changing.

3 Answers

Maybe I posted in the wrong spot, but the second part of the task asks: "Now I'd like to see only groups that are trios, you know 3 members.

So can you please only print out the trios?"

Krishna Desai
Krishna Desai
7,977 Points

You're making the challenge more complicated than it needs to be. All you're asked to do is print out the group members with commas in between them.

If you have ["Run", "DMC", "Jam Master Jay"], the final output should be "Run, DMC, Jam Master Jay". Since it is a multi-dimensional array, when you iterate over the first dimension, your variable will hold a single-dimensional array that can be used with the str.join() method.

Efaz Khan
Efaz Khan
5,194 Points

I did mine like this

#Task 1
all_members = []
for groups in musical_groups:
    all_members += groups

print(", ".join(all_members))

#Task 2
trios = []
for groups in musical_groups:
    if len(groups) == 3:
        trios += groups

print(", ".join(trios))