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

can i have idea about that quiz?

help me please

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

3 Answers

Hi Gavin, you are correct, my bad, here is a different one

That's basically how I did it in the end (though your solution is a bit shorter than mine!)

def combo(first, second):
    length = len(first)
    tuples_list = []
    for i in range(length):
        new_tup = first[i], second[i]
        tuples_list.append(new_tup)
    return tuples_list

Hi Gavin, in most cases length has no meaning (unless you are doing some extra stuff in your code) here is a shorter way and even that can be written in a shorter way

def combo(l,r):
    return [(l[i],r[i]) for i in range(0,len(l))]

but it is better to understand your code/or others code than to make it shorter and not so "readable"

I like that version :) very succinct!

Oh I know there was no need to define length as its own variable, just in my head it felt a bit messy to write range(len(first)). It's probably just a quirk of mine more than anything!

here is an example:

def combo(l,r):
    merged = []
    for i in range(0,len(l)):
        merged.append((l[i],r[i]))
    return merged

If you use the zip function it doesn't count is as correct (I know this because I've just tried it!)