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!)