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

Nick Meier
Nick Meier
1,905 Points

Can the indexing of a list inside a loop be changed by typing [+1] for example?

So I got this step of the final Code Challenge of the Introducing Lists in Python course right, but I switched some symbols and numbers here and there, and now I not completely sure anymore if this is a reasonable way to go about this, or if there is a better method.

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"],
]
# Your code here
for musical_groups[0] in musical_groups:
    print(", ".join(musical_groups[0]))
    musical_groups[+1]

1 Answer

andren
andren
28,558 Points

No, that line of code does not actually do anything useful. And while your code does indeed pass the first task it is likely not doing what you think it does, the fact that it works correctly is actually more of a lucky accident.

First I'll explain how a for loop works, as your code suggests that you might have misunderstood it slightly. A for loop takes a list which you define after the in keyword, and assigns the items within it to a variable that you specify before the in keyword. The loop will automatically pull out the first item during the first iteration of the loop and assign it to the variable, then during the second iteration of the loop it will pull out the second item and assign it to the same variable, and so on until it has gone through the entire list. In other words a for loop takes care of pulling out items for you, which means you don't have to worry about the indexing yourself.

This means that in the code you have written you are asking the for loop to place the items it extracts from the list into the first index of the list, and then you print out that index within your loop. This technically works but means that you are modifying the list itself which you were not really supposed to do.

The proper way of solving the first task is to simply create a new variable for the for loop to store items in and then joining that variable without worrying about indexes at all. Like this:

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 nested_list in musical_groups:
    print(", ".join(nested_list))

The second task similarly does not require you to worry about indexes, you just have to figure out the length of nested_list and conditionally run the print function based on that.

Nick Meier
Nick Meier
1,905 Points

Well, I guess there is some catching up for me to do in this list course. But man, this really is an awesome answer, thank you so much for taking the time to explain everything in detail! This helped me a lot to finally understand the way a for loop works, and what exactly it is doing. Thanks!

andren
andren
28,558 Points

No problem :smile:. Honestly for loops is something a lot of students struggle with when they start out with programming. It was something that confused me a bit as well when I started out coding.

But once you wrap your mind around them they become extremely useful and versatile.