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

Frank Sherlotti
PLUS
Frank Sherlotti
Courses Plus Student 1,952 Points

Combo challenge, need some explanation please.

QUESTION: --- 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. ----- My main question here is how does the enumerate() method work? I've watched the prior videos a few times now and nothing seems to want to click in my head. I was just looking for someone to maybe explain it a little more. And also it says it takes 2 iterables, does that mean the same thing as an argument? Silly question but I figured i'd ask it anyways considering I should probably know the answer already by now. All help is greatly appreciated!(:

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.

def combo(first, second):
  for index, value in enumerate(first, second):

1 Answer

Mari Johannessen
Mari Johannessen
12,232 Points

The enumerate() function adds a counter to an iterable. It's one of the built-in Python functions. It returns an enumerate object. It allows you to create a for loop where you’re iterating over a sequence (list, tuple, etc) and have both the item and it's index in the loop. 2 iterables in this example means that you're iterating over two items, and that both the first and the second item and their indexes will be in the loop. Hopefully that makes a bit more sense, good luck! :)