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

Combo Code Works, but i am just curious how my code can be improved.

Hi All

This code passes the challenge no problem, but it would be great to get some input if there is any improvements or simplifications that can be done to my code.

def combo(arg1, arg2):

    tuple_1 = ()
    list_1 = list(arg1)
    list_2 = list(arg2)

    x=-1
    for item in list_1:
        x+=1
        tuple_1 += ((list_1[x],list_2[x]),)
    list_return = list(tuple_1)

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

2 Answers

AJ Salmon
AJ Salmon
5,675 Points

I like they way you did it, even though it is a little messy. It's a bit hard to read imo, but hey, it works. And looking for ways to simplify is awesome. There are a few ways to do this, and there's probably an even simpler way to do it than I did it, but here's my version:

def combo(itr1, itr2):
     listey  = []
     num = 0
     for item in itr1:
        listey.append((itr1[num], itr2[num]))
        num += 1
     return listey

Thanks for your quick reply AJ, Your version is way cleaner then mine! and easier to read. I am always amazed how Professionals can simplify code so much.

Ashleigh Plummer
Ashleigh Plummer
11,902 Points

Here is another solution:

def combo(iter1, iter2):
    tup_list = []
    for count, val1 in enumerate(iter1):
        tup_list.append((val1, iter2[count]))
    return tup_list

Hope this helps!