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

Oszkár Fehér
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Oszkár Fehér
Treehouse Project Reviewer

I don't try to cheat but what the example shows this code do the job

i just want to know if this code it's at list ok for the ex. what is mantioned in the quiz.

combo.py
# combo([1, 2, 3], 'abc')
# Output:
# [(1, 'a'), (2, 'b'), (3, 'c')]

def combo(*args):
    az_en_listam = []
    for a, b in args:
        for x in enumerate(b, start=a[0]):
            az_en_listam.append(x)

    return az_en_listam

1 Answer

Chris Howell
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Howell
Python Web Development Techdegree Graduate 49,702 Points

Hi Oszkár Fehér

So the code you posted is running into a ValueError: too many values to unpack error.

When args is attempted to be unpacked inside your for loop.

# Modified for example
def example_combo(*args):
    for a, b in args: # Right here we try to unpack args into TWO variables
        print(a)
        print(b)
        # Except we will get a ValueError if its not unpacking right amount.

# Kenneth gives an example of what he will call your function with.

combo([1, 2, 3], 'abc') # notice you are passing two arguments
# but each argument is technically a "packed" item containing 3 items in it.

So you are looping through each thing in args which first time through the loop will be [1, 2, 3]. It tries to unpack [1, 2, 3] into a, b but it can only get 1 and 2 out and what happens to 3? ValueError :(

If you are always expecting 3 items to unpack you could try adding another variable in for loop unpacking.

def example_combo(*args):
    for a, b, c in args: # Now we unpack into 3 variables
        print(a)
        print(b)
        print(c)
# Output should be
1
2
3
a
b
c

This isnt the answer, but it is meant to help you figure out the problem so you can get your code working. Let me know if this helps you understand whats happening.

Oszkár Fehér
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Oszkár Fehér
Treehouse Project Reviewer

I remade the code and i understood that. at the beginning i understood the question wrong and the example to. but after i realized that it needs 2 args.Thank you for your patience to explain it.