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 Tuples With Functions

Vidhya Sagar
Vidhya Sagar
1,568 Points

Code challange,COMPLETELY LOST.

I dont know where to start in this Tuples With Functions Code challenge.

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.

This is the question.Someone Please help.

3 Answers

You can use the zip function for this.

Step-by-step instructions:

  • Define a method that takes two arguments.
  • Zip those two arguments.
  • Convert the zip object to a list and return the final result.

If you follow the instructions correctly, you should end up with this:

def combo(x, y):
    return list(zip(x, y))

Good luck! ~alex

Vidhya Sagar
Vidhya Sagar
1,568 Points

Thanks a ton bro.:)

No problem :)

Nathapong Narumitrekagarn
Nathapong Narumitrekagarn
1,663 Points

Thank you!

But I don't believe that we have learned 'zip' yet at this point. Is there supposed to be a method where we use 'enumerate' or 'item' to complete the challenge?

Vidhya Sagar
Vidhya Sagar
1,568 Points

Yes you can @Nathapong Narumitrekagarn. I was Stuck up for quite some time in this stage .See this code ,hope it satisfies you .

def combo(x,y):
    op=[]
    for idx1,val1 in enumerate(x):
        for idx2,val2 in enumerate(y):
            if idx1==idx2:
                temp_tuple=val1,val2
                op.append(temp_tuple)
    return op
Nathapong Narumitrekagarn
Nathapong Narumitrekagarn
1,663 Points

Thank you so much! I'm happy to say that I've had the same idea (creating a tuple when the indices are a match!), but I didn't know how to separate the two indices and values apart as I kept using plain 'index' and 'value' for both sets of lists.

Cheers

Vidhya: your version is unnecessarily complicated - you don't need to use another loop, since you get the index value for both items from the first loop. (That was the task: you are looking for the item in the second list/iterable which has the same index as idx1, so you can simply refer to it as list2[idx1], and there's your second item for the tuple.)

A more advanced thing, but you should always think twice before using double-nested loops in situations where you could use a single one, because their running time will grow much faster as the input size gets larger (their so-called time complexity will typically be quadratic instead of linear).

Also, the variable name temp_tuple might be unfortunate, since those tuples are not temporary ones whose values are only used inside the block - on the contrary, they are the ones that will actually end up on the output list, untouched.

This is a more concise implementation of your approach (of course, in a real-life situation, zip() would be the most "pythonic" solution, as mentioned above):

def combo(iterable1, iterable2):
    output_list = []
    for index, item in enumerate(iterable1):
        output_list.append((item, iterable2[index]))
    return output_list

Keep up the good work :)