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 Multiple Return Values

gabrielle moran
gabrielle moran
1,985 Points

can combo() be solved using enumerate()?

I created the function successfully using a for loop and idices and adding to an empty list, but there must surely be a more elegant way to create the function - maybe by using the enumerate method?

1 Answer

Robin Goyal
Robin Goyal
4,582 Points

You won't be able to use enumerate for this problem since enumerate is meant for retrieving the index and the item of a single iterable that you're iterating through. In this case, the example shows combo([1, 2, 3], 'abc') so you might be able to do it using enumerate but it won't cover every situation like combo('bob', 'abc').

However, there is a useful function in python which can combine multiple iterables into a iterable of tuples as such.

>>> iter1 = [1, 2, 3, 4]
>>> iter2 = [4, 5, 6, 7]
>>> list(zip(iter1, iter2))
[(1, 4), (2, 5), (3, 6), (4, 7)]

The zip function returns a generator-like object (not sure if you've learned them yet) which need to be converted into a list to view them. So you could complete the combo function as such:

def combo(iter1, iter2):
    return list(zip(iter1, iter2))

But it won't accept this solution, since it's a "shortcut", haha.