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

eestsaid
eestsaid
1,311 Points

How to extract lists of length x from 2 dimensional lists

Team

I'm undertaking task 2 of Code Challenge titled Multidimensional Muscial Groups. The task requires only groups with 3 members to be returned. I am having difficulty moving forward (code for task 1 is below).

Two ways forward I was working on was

  1. define a new function that creates a variable which inludes lists that are of a length = 3.
  2. use an if statement to remove items from a copy of the list.

Do either sound like valid approaches or am I well off track?

Thanks

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

group_members = 1
for group in small_group:
    print("Group #{}: {}".format(group_members, (", ".join(group))))
    group_members += 1

2 Answers

Cameron S
Cameron S
20,537 Points

Hey! Your #2 strategy is heading in the right direction but it sounds like you are overthinking the challenge a bit. Yes, use an if statement but there is no need to create a copy of the list and remove the items from it. You can simply iterate over musical_groups and use the if statement to only return the len() of items that = 3. For example:

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

group_members = 1
for group in musical_groups:  # iterate through musical_groups
    if len(group) == 3:  # logic to return appropriate items
        print("Group #{}: {}".format(group_members, (", ".join(group))))
        group_members += 1

"""
# Output
# Running code returns musical_groups w/ only 3 members

>>> python groups.py
Group #1: Ad Rock, MCA, Mike D.
Group #2: Salt, Peppa, Spinderella
Group #3: Run, DMC, Jam Master Jay
"""
eestsaid
eestsaid
1,311 Points

Thanks a bunch Cameron. I didn't appreciate how the for loop statement effected the if statement.

So is the sequence that in the for loop statement you create the variable 'group' that takes as its input 'musical_groups' then the if statement checks the new variable 'group' to see if it satisfies the condition?

Hope the question makes sense and thanks again.

Cameron S
Cameron S
20,537 Points

Hi eestsaid, to answer your question:

The for statement does not affect the if statement. We use both tools for controlling the flow of the program.

In this challenge, the for loop iterates over the items in the order that they appear in the group_members list. Since you need to modify the sequence you are iterating over while inside the loop we use the if statement to limit the output from the for loop to just the groups that have 3 members (items within the list).

Below is an example of only using a for loop to get the group member lists and the len(), or the number of items in the list. Breaking it out this way helps me better understand control flow in python, hopefully,​ it is helpful for you as well :):

musical_groups = [
    ["Ad Rock", "MCA", "Mike D."],
    ["John Lennon", "Paul McCartney", "Ringo Starr", "George Harrison"],
    ...
]

for g in musical_groups:
    """
    Gets the list of group members and the total 
    number of members in that list
    """
    print(g, len(g))

# output
['Ad Rock', 'MCA', 'Mike D.'] 3
['John Lennon', 'Paul McCartney', 'Ringo Starr', 'George Harrison'] 4
....
eestsaid
eestsaid
1,311 Points

That is really helpful Cameron. Breaking it out like you did has helped correct a misunderstanding of what was happening. Thanks again.

Cameron S
Cameron S
20,537 Points

Glad I was able to help. Keep up the good work!