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 trialARUN DAMODARAN
733 PointsLIST CHALLENGE 2
Challenge Task 2 of 2 I'd like to see only groups that are trios, you know 3 members. So can you please only print out the trios? It should still use the joined string format from task 1.
Below is the code that I had written and I am getting only the last set in the list which has 3 members.
Appreciate your responses in correcting this code.
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"],
]
for group in musical_groups:
len_counter = 0
if len(group[len_counter])==3:
print("{}".format(", ".join(group)))
len_counter += 1
4 Answers
Logan R
22,989 PointsHi ARUN DAMODARAN,
You are on the right path. Instead of checkings for the length of musical_groups
at the index [len_counter]
, you should just check the length of group
. For example: len(group)==3
.
ARUN DAMODARAN
733 PointsThanks Logan.
I could not follow your response.
Can you please show the code for the above said solution?
Logan R
22,989 PointsSure Arun,
for group in musical_groups:
len_counter = 0
if len(group)==3:
print("{}".format(", ".join(group)))
len_counter += 1
ARUN DAMODARAN
733 PointsLogan,
Yes it did work.
I am still wondering why my former solution did not work?
I was in a impress that If I use len(group) it would result in total length of the list which is 7 in our case. Did not know that len(group) will count the total length inside of the list.
Let me know your thoughts. I am still in learning phase so I might ask some basic questions.
Logan R
22,989 PointsHi Arun,
When you do a for loop, you do for ITEM in LIST
. As the for loop goes through the list, item becomes the next element in the list.
In this case, each time we complete one loop of the for loop, the group
variable is updated to the next array in the list.
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"],
... ]
>>> for group in musical_groups:
... print("Item:", group)
Item: ['Ad Rock', 'MCA', 'Mike D.']
Item: ['John Lennon', 'Paul McCartney', 'Ringo Starr', 'George Harrison']
Item: ['Salt', 'Peppa', 'Spinderella']
Item: ['Rivers Cuomo', 'Patrick Wilson', 'Brian Bell', 'Scott Shriner']
Item: ['Chuck D.', 'Flavor Flav', 'Professor Griff', 'Khari Winn', 'DJ Lord']
Item: ['Axl Rose', 'Slash', 'Duff McKagan', 'Steven Adler']
Item: ['Run', 'DMC', 'Jam Master Jay']
ARUN DAMODARAN
733 PointsGot it Logan. Thanks for your explanation.