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

python help

dont know what to do

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

def combo(a,b):

2 Answers

Eray Ates
Eray Ates
14,292 Points

Lets start code with fuction declaration

def combo(my1, my2):

First you need to an iteration to get each tuple like as (1, 'a')

You can use it a function so if I pass 1 and 'a' I can get tuple:

def combo(my1, my2):
    def tuplela(x,y):
        return (x, y)

Now we get our core function, but we need to pass each x and y value one by one,

So map function can help us, it can get more than one iterable value and pass it to a function.

map(tuplela, my1, my2) #this get values of x,y from my1 and my2 one by one

But it will calculate so you want to get result should turn it to list

def combo(my1, my2):
    def tuplela(x,y):
        return (x, y)
    return list(map(tuplela, my1, my2))

And here it works. Also you can turn tuplela function to a one line function ( as we know name lambda)

tuplela = lambda x, y: (x,y)

So you don't have to declare it just put in map

list(map(tuplela, my1, my2)) --> list(map(lambda x,y: (x,y) ,my1, my2))

def combo(my1, my2):
    return list(map(lambda x,y: (x,y) ,my1, my2))

And this is our final solution, bring it together.

If you haven't gotten up to map and lambdas, then perhaps just try creating a temporary variable to hold your output list, and then loop through one of the lists and just add to the temp list the value from the first list at that index, and the value of the second list at the same index. If you add two values separated by a comma, it should be a tuple by default. I think the parentheses are really just to help readability (so it's not a bad idea to use them also).

Once the loop is finished, return the temp list that should now contain the pairs as a tuple at each index.

Good luck!