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

Asher Orr
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Asher Orr
Python Development Techdegree Graduate 9,408 Points

I created a variable that joins the list musical_groups, but I get a TypeError when I use it in my code.

Hi everyone! I am completing this code challenge:

"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?"

I tried this code, but I got a TypeError:

File "", line 11, in TypeError: sequence item 0: expected str instance, list found.

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"],

# My code is below:
group_number = 1
group_members = ", ".join(musical_groups)
for group in musical_groups:
    print("Group #{}: ".format(group_number) + group_members)
    group_number += 1

I looked up an older post on the Treehouse forums that inquired about this error, and someone replied with this:

"You are taking out each group and turning it into a string meaning when you loop over group_members you are in fact looping over characters and not elements in lists. You need to do something like this:"

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

Then I went and used this code:

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

This code worked, but I would like to better understand why. Why can't I use a variable to join the elements in the list together, then insert that variable into the print statement?

Thank you so much for reading!

1 Answer

Steven Parker
Steven Parker
231,007 Points

You could use a variable, you just need to create it within the loop for each group:

for group in musical_groups:
    group_members = ", ".join(group)    # creating the variable here works fine
    print("Group #{}: ".format(group_number) + group_members)
    group_number += 1

Both this and your final code join "group", which contains a list of names. The original code was trying to join "msuical_groups", which contains a list of other lists.

Also, this challenge only asks for the joined lists without numbering them. Doing things other than what the instructions ask for can sometimes confuse the checking system.