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 trialmatildadigital
4,947 PointsCombo() Function That Returns List of Tuples
The error message I received was "Where's 'combo()'?" I've deleted the lines of code to see if I could isolate an error in one of the other lines, but the error messages didn't lead me anywhere.
# 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(list1, list2):
for item in list1:
my_tuples_list.append(list1[item], list2[item])
return my_tuples_list
2 Answers
Kourosh Raeen
23,733 PointsHi matildadigital - Before starting the for loop define the list:
my_tuples_list = []
That will take care of the "Where's combo()?" error. But there are other issues with the code. The loop variable item
holds the items in list1, but you're using it as in index for both lists. Try changing your loop to:
for index in range(len(list1)):
Now you can use index
as an actual index of items in both lists.
Last issue is that you need to return a list of tuples so before doing the append use parenthesis to make a tuple out of the two items and then append the tuple to my_tuples_list
.
jcorum
71,830 Pointsmatilda, yes, the error seems totally spurious. Here, try this:
def combo(iter1, iter2):
combo_list = []
for index, value in enumerate(iter1):
tuple = value, iter2[index]
combo_list.append(tuple)
return combo_list
darcyprice
4,442 PointsThank you Jcorum! :)