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 trialNivesh Sharma
Courses Plus Student 1,941 PointsHow 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?
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
Treehouse TeacherHi 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!
Timothy Long
Front End Web Development Techdegree Student 6,833 PointsYou 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!
Nivesh Sharma
Courses Plus Student 1,941 PointsThanks jennifer, It so;ved my problem and I understood the concept..
Timothy Long
Front End Web Development Techdegree Student 6,833 PointsTimothy Long
Front End Web Development Techdegree Student 6,833 PointsOops, 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.