Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Collections (2016, retired 2019) Tuples Combo

John Mclane
PLUS
John Mclane
Courses Plus Student 3,099 Points

create a function named combo function: Do you have a different solution? Here is mine!

I came up with a viable solution that passed. However, I wanted to get your take on what worked for you all? I don't know if was supposed to use the *args. I'd love to know how you all tackled the challenge. Thanks in advance!

combo.py
# example I used to test was 
# first_iterable = 'tre'
# second_iterable = [1,2,3]

# My logic was that I wanted to split the given iterables into a list format
# then take each element of first_iterable list and second_iterable list and create a tuple 
def combo(first_iterable,second_iterable):
    list_of_things = []
    first_iterable_list = list(first_iterable)
    second_iterable_list = list(second_iterable)
    index = 0
    for value in first_iterable_list:
        first_iterable_value = first_iterable_list[index]
        second_iterable_value = second_iterable_list[index]
        tuple_value = (first_iterable_value, second_iterable_value)
        list_of_things.append(tuple_value)
        index += 1
    return list_of_things

1 Answer

Steven Parker
Steven Parker
215,953 Points

Good job! :+1: You've implemented the classic strategy.

Optionally, there's a few things you can do to make the code more compact:

  • the iterables don't need to be converted into lists
  • the loop could use "index" directly with a range based on the length of either argument
  • the indexed items can be made into a tuple directly (without intermediate variables)

Also, in case you didn't know, this function implements essentially the same thing as the built-in function named "zip". But of course, the challenge doesn't allow you to use that in your solution!