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 - Musical Groups Challenge Task #1

The instructions to this assignment are to "loop through each group and output the members joined together with a ", " comma space as a separator." I have tried the following code to do such. On this following one, I recognized that I need to increment to the next list in the loop:

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

for groups in musical_groups:
    group_members = ", ".join(musical_groups[0])
    print(group_members)

So, I went ahead, and added the following to my for loop

    musical_groups += 1

Obviously, this is not right, but I am stuck as to continue my code to run through each line.

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,468 Points

You very close to correct! There is no need to increment an index into musical_groups. The for loop automatically assigns each group in turn to your variable groups. Changing the join statement as below will complete Task 1:

    group_members = ", ".join(groups)

Not needed for the challenge, but for style points, it helps make the code slightly more readable if the variable name agrees more closely with the object it refers to. In this case, group may be a better choice as the for loop variable since only a single group is assigned each loop.

Post back if you need more help. Good luck!!!