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

Rakesh Bharadwaj
Rakesh Bharadwaj
1,376 Points

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

Boris Ivan Barreto
Boris Ivan Barreto
6,838 Points

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
Bruno Aldo Lunardi
Bruno Aldo Lunardi
15,795 Points

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

Viktor Léhner
Viktor Léhner
12,771 Points

Very nice and simple solution!

Steven Parker
Steven Parker
231,269 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))]