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

Brain is hurting...Musical groups Challenge Task 1

Here is a multi-dimensional list of musical groups. The first dimension is group, the second is group members.

Can you loop through each group and output the members joined together with a ", " comma space as a separator, please?

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

I'm pretty confused on how to even approach this challenge. My code is probably no where near where it needs to be. Any help would be appreciated.

Sorry, I only see group members. I see that it's in a multi dimensional array, or dictionary, but are we supposed to name the group ourselves? I'm a little confused at the question.

4 Answers

Hey Ira! I remember the struggle, but keep the courage, it'll get better! That being said, your right, youre no where near the answer:D!

A general rule with the code challenges on Treehouse is, never ever do something unless you were explicitely told to do so. For instance in the beginning of your code you have wrote "print("Musical Groups:")" And while i can see what your trying to do, you werent told to do this, and so even if your solution was correct you wouldent pass.

Now i think you can do this so i want give you the answer, but ill give some hints.

The variable that you have declared "group_name" is never used for anything, and so why dont you just remove it?

When using .join, your trying to join a list together with an operator and it should look something like this:

"whatToJoinWith.join(theListYourTryingToJoin)
# So for instance say we have this list:
myList = ["a", "b", "c"]
# You could then do
", ".join(myList)
# Would return "a, b, c"

Finally, lists in python have a function called len(), which will return the length of a list, so for instance in our previous example:

myList = ["a", "b", "c"]
len(myList)
# This would return 3 since the list conatins 3 items

In the second challenge it wants you to check if the length of the specific group is 3, and only if so print it.

Hope this helps! Please write back if you need more help tho!

Ok so after reading and rereading and developing a headache, and then taking some ibuprofen, and then rereading I realized that I may either be dyslexic or have underlying dyslexia that I just discovered... For some reason, I thought the first name in each row was the name of the group... I don't know why but it took me a while.

The .join() method is pretty simple to understand after watching the video and practicing. However, I do not know where to begin joining these lists within a list.

Haha Ira, love the enthusiasm, i dont see that often. Sry it took me so long to come back, and further more your headache is completely on me mate! I thought you were on the second step of the challenge, but now i realise you are on the first task, so ignore everything i have said.. So sry about this.

Now to your issue.

The challenge asks you to use a for loop to loop thru each item in the list "musical_groups". Seeing as this is a list of lists, each item will be a list.

Now if you do the following:

for group in musical_groups:
    print(group)

Your output will be all of those sublists inside musical_groups. Now all you want to do, is join each "group" with the string ", ".

So just copy my code as a template, but then instead of just printing group, it wants you to print "group" join by a comma and a space.

Hope this helps! Write back if it dosent, and ill be quicker this time!:P

Yea I ended up finding the answer. I understood it when I just copied and pasted the answer but later on forgot so it was good to come back and have your response as a bit of a refresher. I could use as many refreshers as I can get! Thanks!!

Glad you figured it out Ira. Happy coding!

Here is my solution:

for group in musical_groups:
    print(", ".join(group))

Thanks for that. I was getting a headache there, too.

Tried it, it really works!!! HA! Headache cure ;)

I dont know but i still dont understand how to "loop through each group and output the members joined together with a ", " comma space as a separator. Can you please help with this ?

Hey Artjom! If you feel like you don't understand any of the concepts needed to solve the challenge, I would strongly recommend that you rewatch some of the previous video, as they explain it in depth. Particularly you need to understand for loops and the .join method.

Hope this helps!

group1="" for group in musical_groups:

   group1 +=  ", ".join(group)

print((group1))