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

4,333 Points

Converting a list of lists into a string to join

I'm not sure why I'm having trouble here but despite my hunting on google for solutions to avoid asking here, the best solution I could come up with was appending the lists within the list, which while it gives the desired output of separating the various items with a ", ", doesn't use the .join function which is exclusive to strings.

After trying it a few other ways I just kept coming up blank, for some reason my brain just doesn't want to process turning lists within a list into a string. I'd originally tried using something like this to start.

group = ", ".join(musical_groups) #A string if I'm not horribly mistaken, though musical_groups is a list of lists, so I'm assuming the error is in relation to that.

but it always returned for me an error of "TypeError: sequence item 0: expected str instance, list found." Of course, I can add a sequence item, but only 1 like group = ", ".join(musical_groups[0]) or any other number in the sequence, but only a single argument (I thought I remembered somewhere, something about being able to do something like .join(musical_groups[0][6]) where the second number was indicating the end half of the location ranges it would run through to join. Apparently that's not the case in this particular function with how I'm approaching it. Chances are good I'm probably just overthinking the problem, but sometimes it takes a while for the solution to coalesce.

Obviously i'm missing something because I'm stuck trying to convert the list of lists into a single string apparently, or maybe I'm just interpreting the problem incorrectly, or both.

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

for group in musical_groups:
    for group_members in group:


2 Answers

Jennifer Nordell
Jennifer Nordell
Treehouse Teacher

Hi there, horus93 ! You've clearly given this a lot of time and thought and even some research! I give you an A++ for effort. But you're at the point where you've overthought this, I believe. Can we agree that there is one outer array which contains all groups and each element in there is an array containing people that make up that individual group?

So here was my solution and I will explain:

# for each group in all groups
for group in musical_groups:
    # take all the members of that group and print them as a string separated by commas
    print((", ").join(group))

So, for every group in our outer musical_group join the members of that group with a comma and print it as a string. The first iteration, group will be equal to ["Ad Rock", "MCA", "Mike D."] and a line will be printed that says Ad Rock, MCA, Mike D..

My gut instinct is to say that you probably had something similar to this at one time, but very likely your print statement wasn't indented so it was outside the loop. That's a very common pitfall.

Hope this helps! :sparkles:

4,333 Points

Thanks again for your awesome help Jennifer, and yea, I was way overthinking it which usually tends to lead towards frustration lol. Took me a few days to get past one of the other ones because of that, until going back over some previous lesson videos and the solution struck me. Here however, I kept coming up in a wash haha.

You were right though, in that at one point (i think earlier in the process), but something I was doing kept it from being a string (can't remember anymore at this point), so that's why I kept looking around for solutions (but in the end, the solutions are generally already here right in front of me, and for some reason my mind was focused on needing some sort of extra step to iterate through the sub arrays and join them together. Thanks for shedding some light on and helping lift the veil for me, the second step for printing only groups with 3 members only took about 30 seconds for me to remember the code solution for, and another 30 seconds to fix it after i put it in wrong on the first attempt (though thankfully, without any backtracking and reviewing :) ). In the meantime I'm having fun learning about algorithms on Khan academy, because it's a bit more extensive, though ill probably blow through the section I saw in the library here as well, it's a fascinating subject.