Treehouse Challenge

IB Year 1 HL Computer Science
Forum rules
Please be careful about sharing personal information. Never use any identifiable information.
Post Reply
User avatar
AleksanderHD
New User
Posts: 1
Joined: 17 Sep 2018, 10:14

Treehouse Challenge

Post by AleksanderHD » 18 Oct 2018, 20:49

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?

Code: Select all

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"],
]
I am not sure how to get started with this problem. I know how to separate the names in each list (musical_groups[0].split(", ")) but I don't know how I need to loop it through each different band changing the number of the list in the hard brackets.
1 x
► Show Spoiler
Dm me for Deep Fried Memes

- Aleksander HD

User avatar
Mr. MacKenty
Site Admin
Posts: 88
Joined: 28 Apr 2018, 17:06
Answers: 3

Re: Treehouse Challenge

Post by Mr. MacKenty » 18 Oct 2018, 21:39

Hello :lol:
  • When asking a question, please help me by telling me / showing me what you have tried
Multi-dimensional arrays are tricky! in the example below, we see this list has only 7 items. but INSIDE each item there are 3 or 4 additional items. So if I typed:

print(musical_groups[0]), I might see:

["Ad Rock", "MCA", "Mike D."]

So if I want only the first name in the first group, I would need to type this:

print(music_groups[0][0])

The code above is saying slice into the first item in our list AND THEN slice into the first item inside of the first item.. I admit, it's a little tricky at first. I'll go over this in class to help you understand it all. I'm not 100% sure, but I think split only works on strings. I'm not sure it works on lists (but I might be wrong).

Code: Select all

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

#
# this code IS NOT TESTED!!!!
#

# on the line below I'm simply looping through each item in the list
for i in musical_groups:

# the next line I'm looping again, but this time through the actual band members
    for j in len(i):
    
    # finally, I print the band name inside the loop. 
        print(i[j]," , ")
      
      
# again, this isn't tested. But I think it should help you get started.       

AleksanderHD wrote:
18 Oct 2018, 20:49
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?

Code: Select all

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"],
]
I am not sure how to get started with this problem. I know how to separate the names in each list (musical_groups[0].split(", ")) but I don't know how I need to loop it through each different band changing the number of the list in the hard brackets.
1 x

Post Reply