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

Bruce Röttgers
Bruce Röttgers
18,211 Points

Error: 'Too many values to unpack'

On line 6 (for loop) it says too many values to unpack, so this is probably the false route, but I don't really get it what I have as other options,

combo.py
# combo([1, 2, 3], 'abc')
# Output:
# [(1, 'a'), (2, 'b'), (3, 'c')]
def combo(itera_1, itera_2):
    buffer = []
    for first, second in itera_1, itera_2:
        buffer.append((first, second))
    return buffer

1 Answer

Kent Åsvang
Kent Åsvang
18,823 Points

There is a lot of cool solutions to this. I would use the zip-function myself:

    def combo(a, b):
        return list(zip(a, b))

The zip function takes two iterables, and returns an iterable of tuples with the corresponding values. You can read about it here zip-function

Bruce Röttgers
Bruce Röttgers
18,211 Points

The challenge shouldn't be solved using zip() (and can't)

Kent Åsvang
Kent Åsvang
18,823 Points

I'm not a paying student, so I can't look into the challenge and didn't know you weren't allowed to use the zip-function. Anyway, here is another solution you could try:

def combo(a, b):

    # create empty list
    buffer = list()

    # check that the length of the iterables are the same
    assert len(a) == len(b)

    # run a for-loop for every value in a
    for i in range(len(a)):
        # create a tuple with the corresponding values from each iterable, using indexing
        my_tuple = tuple([a[i], b[i]])

        # append the tuple to the empty list
        buffer.append(my_tuple)

    # return list of tuples
    return buffer

Hope this helped then. Sorry for the initial mistake. And in case you wan't a pretty oneliner, you could use this:

def combo(a, b):
    return [tuple([a[i], b[i]]) for i in range(len(a))]