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 (Retired) Tuples Combo

Gilang Ilhami
Gilang Ilhami
12,045 Points

Combo

Hmm i don't really now what's wrong

It says that i have a SyntaxError: def combo([1, 2, 3], 'abc'):

isn't that base on the instruction?

zippy.py
# 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([1, 2, 3], 'abc'):
    new_list = []

    num_list = [1, 2, 3]
    string = 'abc'

    for letter in string and num in num_list:
        output = (letter, num)
        new_list.append(output)

    return new_list

3 Answers

Ryan S
Ryan S
27,276 Points

Hi Gilang,

As I mentioned above, you don't need to pass in the values to the function. They are only there for example. Only use generic arguments. For example: iterable1 and iterable2.

We know that the iterables are the same length, so if we loop through one and keep track of what index position we are at, we can then use that position to get the matching item in the second iterable. We can use a count variable called "position" to keep track of what index position we are at. It will start at 0 because that is position of the first item, and it is defined outside the loop.

You can do this in less lines of code, but I've included it all for clarity.

def combo(iterable1, iterable2):
    new_list = []
    position = 0

    for item1 in iterable1:
        item2 = iterable2[position]
        output = (item1, item2)
        new_list.append(output)
        position += 1

    return new_list
Ryan S
Ryan S
27,276 Points

Hi Gilang,

The comments in the code challenge are only there as an example. They don't want you to actually pass in the values that they are suggesting, just use them as hint so you know what is expected if you were to pass them in. That is what is causing your syntax error.

Your function should take 2 generic arguments. You don't know exactly what they will be, only that they will be iterable and of equal length. So they could be a list of numbers, or a list of strings, or a single string, etc.

The next thing is that your syntax for your for loop will not work as you are intending. Although it is possible to use single-line nested loops in Python, it's not covered in these courses and not necessary to solve this challenge. It is an interesting way of shortening code though and it is worth looking into as you get more experience with Python.

Just to give you a couple hints on the challenge:

There are a few ways to solve this one, but you can do it using one for loop. Think about how the indexes of the iterables could help you match them up.

Hopefully this clears things up a little bit. If you need more help let me know. Good luck.

Gilang Ilhami
Gilang Ilhami
12,045 Points

Hey Ryan, thank you so much for the replay

i tried to change up the codes, but i still have a hard time understanding it Mind take a look?

def combo([1, 2, 3], 'abc'):
    new_values = []
    num_list = [1, 2, 3]
    string = 'abc'
    for num, letter in enumerate(num_list, string):
        output = [letter, num]
        new_values.append(output)
    return new_list