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

Create a function named combo that takes two ordered iterables. These could be tuples, lists, strings, whatever.

Create a function named combo that takes two ordered iterables. These could be tuples, lists, strings, whatever.

I could get the exact output as required

combo.py
# combo([1, 2, 3], 'abc')
# Output:
# [(1, 'a'), (2, 'b'), (3, 'c')]
def combo(*args):
    b =list(args)
    #print(b)
    d = list(b[0])
    c = list(b[1])
    e = ()
    f = ()
    i = 0
    while i <= len(b):

        e = (d[i],c[i])
        f += (e,)
        i += 1
    return list(f)

3 Answers

I managed to solve the task this way

def combo(*arg):
    my_result = []
    for index, val in enumerate(arg[0]):
        my_result.append((val, arg[1][index]))
    return my_result

I had quite a tough time thinking about a shorter solution for this one and you provided such an elegant and simple solution here, thanks !!

Boris Ivan Barreto beautiful!! thanks for the hint

Very nice and simple solution!

Steven Parker
Steven Parker
241,809 Points

Boris's solution is clever, but rather unconventional and complicated. Much more concise solutions are possible, and it's also not necessary to use "enumerate" or the unpacking operator. For example:

def combo(a, b):
    return [(a[i], b[i]) for i in range(len(a))]