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, s

can any one explain me how did this code working

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

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

1 Answer

Steven Parker
Steven Parker
231,236 Points

While this works, it's a rather unconventional and complicated solution. It's not necessary to use "enumerate" or the unpacking operator. What the solution above does can be simplified like this:

def combo(a, b):
    my_result = []
    for index in range(len(a)):
        my_result.append((a[index], b[index]))
    return my_result

With the extra complexity removed, the operation should be a bit more clear.

I'd guess you found this in an old question from 2017. You might want to see the even shorter solution I posted there. It also works the same way but uses list comprehension syntax to be more compact.

thanks Steven parker