Python Python Collections (2016, retired 2019) Tuples Combo

John Mclane
PRO
John Mclane
Pro 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
203,990 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!