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 (Retired) Tuples Combo

No clue how to program with tuples

Where do I start with the combo problem? I don't know how to take the combo input and split it into the parts I need for output.

zippy.py
# combo(['swallow', 'snake', 'parrot'], 'abc')
# Output:
# [('swallow', 'a'), ('snake', 'b'), ('parrot', 'c')]
# If you use list.append(), you'll want to pass it a tuple of new values.
# Using enumerate() here can save you a variable or two.
akak
akak
29,445 Points

You need a function that accepts two input parameters, an empty list to put the result into, and a for loop to loop through input parameters and add them to the empty list. I can share how I solved if you're really stuck. The good idea is to make a for loop and print key and value to see what's inside and how you could work with it.

1 Answer

Martin Cornejo Saavedra
Martin Cornejo Saavedra
18,132 Points

I got two solutions, the common one and the cool one:

#Common solution
def combo(iter_1, iter_2):
    iter_output = [];
    for idx, value in enumerate(iter_1):   #iter_2 will work too
        iter_output.append((iter_1[idx], iter_2[idx]))   #appending a tuple

    return iter_output

#--------------------
#Cool solution
def combo(iter_1, iter_2):
    return list(zip(iter_1, iter_2))

zip() is a built-in function from python that accepts two iterables of the same length and joins them on a zip object. If you use list() on a zip object you obtain the goal required in this challenge.