Welcome to the Treehouse Community
Looking to learn something new?
Welby Obeng20,340 Points
Is there a better way to solve this Python Code Challenge?
Create a function named combo() that takes two iterables and returns a list of tuples. Each tuple should hold the first item in each list, then the second set, then the third, and so on. Assume the iterables will be the same length.
# 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. def combo(arg1,arg2): final =  arg2_list=list(arg2) for arg1_key,arg1_value in enumerate(arg1): for arg2_key,arg2_value in enumerate(arg2_list): if arg1_key == arg2_key: final.append((arg1_value,arg2_value)) return final
Kenneth LoveTreehouse Guest Teacher
You don't need to turn
arg2 into a list. It's going to be an iterable so you can loop over it anyway.
I'm not sure why you're doing two
for loops. Using
.enumerate() is smart and gives you the index position. Use the value from one of the lists and use the index to get the same indexed value from the other list. Put those into a tuple in the right order and send 'em back.
def combo(iter1, iter2): output =  for index, value in enumerate(iter1): output.append((value, iter2[index])) return(output)
William LiCourses Plus Student 26,865 Points
Hi, Welby, Here's my solution, it makes use of list comprehension and Python's built-in zip function.
def combo(iter1, iter2): return [(i, j) for i, j in zip(iter1, iter2)]