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 trialmichaelabendroth
4,561 PointsHow to iterate over 2 enumerated iterables with a for loop?
I have been stuck on this one for 2 days - checked python documentation and stack overflow and still can't figure this one out. The question asks to create a function that takes 2 iterables and creates a list of tuples with the values from each list. How do you iterate over 2 enumerated iterables? I can't find the syntax for this anywhere so I'm guessing my approach is basically wrong.
# combo(['swallow', 'snake', 'parrot'], 'abc')
# Output:
# [('swallow', 'a'), ('snake', 'b'), ('parrot', 'c')]
# If you use list.append(), you'll want to pass it a tuple of new values.
# Using enumerate() here can save you a variable or two.
def combo(it_one, it_two):
a_list = []
for value in (enumerate(it_one[1]), enumerate(it_two[1])):
a_list.append((value(it_one), value(it_two)))
return a_list
1 Answer
sangsatori
2,761 PointsThis seems to work.
The trick for a short & sweet solution is the fact that you get given two iterables, at the same length - so you can skip iterating over the second one and just access it by index directly; it will be the right value anyway.
You seem to be using iterators incorrectly.
There's also some other confusing parts about your code. Just... take a look at the solution and rethink the steps, good luck!
michaelabendroth
4,561 Pointsmichaelabendroth
4,561 PointsI was driving myself nuts with this one -that's why the code looks so crazy. Thanks for the advice, it makes perfect sense now.