Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Introducing Lists Build an Application Multidimensional Musical Groups

How to solve this challenge. Having trouble in traversing every element in the multidimensional list...Please Help!

how can i use the for loop for traversing through the lists?

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 i in range(len(musical_groups))
    joined_members= ", ".join(i)

print(joined_members)

3 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, nivesh sharma! You will not need a nested loop for this. You can do this with one loop. The challenge is asking you to go through and take each individual group and print out that group joined with a comma and a space.

Here is how I approached it:

# for each group in all groups
for group in musical_groups:
    #print the result of joining that sub-array with a comma and a space
    print((", ").join(group))

Hope this helps! :sparkles:

Oops, I suppose I should have actually taken a look at this challenge! I just took the question at face value and told Nivesh how to loop through every index in a multidimensional list.

You will have to end up using nested for loops to be able to loop through all of the indexes.

For example if I have these lists: stuff = [[1,2,3,4], [5,6,7,8]] to be able to loop through every index I can do the following:

for items in stuff:
    for everything in items:
        print(everything)

The end result of this would be:

... 
1
2
3
4
5
6
7
8
>>> 

The first for loop is only looping through the indexes of the stuff list. So stuff[0] is [1, 2, 3, 4] etc. The second for loop is looping through the indexes inside stuff[0].

Hope this helps!

Thanks jennifer, It so;ved my problem and I understood the concept..