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 Python Collections (2016, retired 2019) Tuples Combo

I am stumped on this one

please help me figure out whats wrong with my code.

combo.py
# combo([1, 2, 3], 'abc')
# Output:
# [(1, 'a'), (2, 'b'), (3, 'c')]
def combo(it1,it2):
    first_lyst = []
    second_lyst = []
    for value in enumerate(it1):
        first_lyst.append(value)
    for value in enumerate(it2):
        second_lyst.append(value)
    lyst = tuple(first_lyst + second_lyst)
    return lyst

3 Answers

enumerate returns a tuple of the index of the value and the value at that index so each value in the loop is a tuple then you are concatenating the two lists of three tuples together to create a new tuple of six pairs of index and value tuples.

you just need to pass the two arguments straight to the zip function this zip function creates a tuple of the correspondingly positioned elements of each list. i.e.

(theFirstIndexOfListOne, theFirstIndexOfListTwo) 

if there were three lists passed to zip there would be three items in the tuple it returns etc.

Then pass the result of the zip function as an argument to the list function to wrap the three tuples in a list then return the list viola

def combo(it1, it2):
    return list(zip(it1, it2))

thanks man that really helped out

Joshua Dam
Joshua Dam
7,148 Points

Zip is a good way to do it... But it doesn't help pass this challenge. It won't allow you to use the zip() function

def combo(a,b):
    the_list = []
    counter = 0
    for item in a:
        the_list.append((a[counter], b[counter]))
        counter += 1
    return the_list
Chris Grazioli
Chris Grazioli
31,225 Points

@Glen Lavery Thanks Glen, you're the man... the only one on the forum that actually answer the question without cheating trying to use a zip() that wasn't introduced yet in the course, or in the spirit of the actual exercise. trying to figure out the whole "(a[counter],b[counter])" thing was driving me crazy tonight

slavster
slavster
1,551 Points

Glen Lavery this was definitely the quickest and easiest to understand answer that I read for the challenge, thanks!

One snag I ran into when trying to write my own code in this style: I got an error saying that append only takes one argument and I had given it two. Instead I just created a new variable (called new_item) and set it to equal (a[counter], b[counter]) and I simply appended new_item to the_list. That seemed to fix the issue, not sure if you ran into that while writing your code.

no problem, glad to help