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 trialJonathan Shedd
1,921 PointsNeed help with a function that returns a list of tuples after unpacking two iterables.
The task is to create a function that takes two iterables and then returns a list of tuples that follows the pattern: [(iterable1[0], iterable2[0]), (iterable1[1], iterable2[1]) ... ]. It keeps returning the error that there are to many items to unpack. Any help is greatly appreciated!
# combo([1, 2, 3], 'abc')
# Output:
# [(1, 'a'), (2, 'b'), (3, 'c')]
# If you use .append(), you'll want to pass it a tuple of new values.
def combo(iterable1, iterable2):
my_list = []
for item, item_1 in iterable1, iterable2:
my_list.append((item, item_1))
return(my_list)
2 Answers
Steven Parker
231,275 PointsYou can't have two iterables in the same for loop.
But you could use the length of one as a range, and have an index you could apply to both.
Chris Jones
Java Web Development Techdegree Graduate 23,933 PointsWhy can't you have two iterables in the same for loop? Below is my code and I keep getting the message "Didn't get the right output. For example, expected (10, 'T') as the first item, got (10, 'T') instead.". This is very strange, because it appears to be getting the right answer, but for some reason it won't let me pass the code challange.
def combo(it1,it2):
answer = []
#i = len(it1)
for item1 in it1:
tup1 = item1
for item2 in it2:
tup2 = item2
tup = (tup1,tup2)
answer.append(tup)
return answer
Nathan Bivens
14,460 PointsI'm having the same issue. Not sure why.
Jonathan Shedd
1,921 PointsJonathan Shedd
1,921 PointsThanks, that was a huge help! I've got it now. :)