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

Hi Team,

Can someone provide me the alternative code for below without using any built in functions.

def combo(arg1,arg2): tuple_list= list() arg3=zip(arg1,arg2) for element in arg3: tuple_list.append(element) return tuple_list

combo.py
# combo([1, 2, 3], 'abc')
# Output:
# [(1, 'a'), (2, 'b'), (3, 'c')]
# combo([1, 2, 3], 'abc')
# Output:
# [(1, 'a'), (2, 'b'), (3, 'c')]
def combo(arg1,arg2):
    tuple_list= list()
    arg3=zip(arg1,arg2)
    for element in arg3:
        tuple_list.append(element)
    return tuple_list
#print(combo([1, 2, 3],[2,5,6]))

2 Answers

Cody Kaup
Cody Kaup
14,407 Points

Since both arguments are the same length, we can loop through one argument and gather the index we're on with enumerate(). Something like this:

def combo(arg1, arg2):
    tuple_list = list()
    for index, element in enumerate(arg1):
        tuple_list.append((arg1[index], arg2[index]))
    return tuple_list

Thanks for your reply. I have an additional question, does .append() takes two argument. From my understanding, it restricts to 1. Please confirm.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,457 Points

The method append() takes exactly one argument. The this case, the argument is a single tuple containing two items.

Thanks for clarifying Chris!!!