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

Why are the results different?

I know that the code is wrong but why am I getting different results in the challenge than workspaces. At the same time I am really confused, could use help on breaking down the problem.

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(value_set, label):
  result = []
  count = 0
  for label, item in enumerate(value_set):
    result.append('{}, {}'.format(item, count))
    count += 1
    tuple(result)
  return (tuple(result))


print(combo(['T','r','e','e','h','o','u','s','e'], 'abc'))

1 Answer

Dan Johnson
Dan Johnson
40,532 Points

When you're using enumerate you're only pulling the values from one of the iterables. With:

for label, item in enumerate(value_set):

label will become the index/count generated by enumerate, and item will be the value in value_set. So the challenge is probably passing in a list of numbers which will cause you to get a list with tuples only containing numbers.

What you can use enumerate for is to generate an index for the other iterable (left and right being the iterables):

  for index, left_val in enumerate(left):
    combo.append( (left_val, right[index]) )