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

Van Sanders
Van Sanders
16,087 Points

Stumped on the 'combo' code challenge...

I totally understand why this code that I have returns the output that it does (All combinations of items between the iterables entered). I'm at a problem-solving brain block and can't seem to see how to use enumerate() to combine these two iterables without using two 'for loops'.

Rather than give me the answer, could someone give me a nudge or two in the right direction?

(Really awesome track by the way. I loved the Ruby track, but the challenges here are a bit more of a learning experience. They ask you to synthesize knowledge gained in the past few classes, which is awesome for retention!)

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(it1, it2):
  tupple_list = []
  for index1, value1 in enumerate(it1):
    for index2, value2 in enumerate(it2):
      tupple_list.append((value1, value2))
  return tupple_list

3 Answers

Hi Van

notice that the length of the string is 3 and so is the list . so when use enumerate with a for loop around the second iterable use the index to grab the values in the first list and use value to create the second part of the tuple, so you can do it in one for loop.

hope that helps.

Alexander Hansson
Alexander Hansson
2,083 Points

Spoiler: This helped me a lot. I ended up comparing the index1 and index2 so they are the same. :) It worked.

Boris Ivan Barreto
Boris Ivan Barreto
6,838 Points

Hi,

You can apply enumerate() on the string and you get an index and a value, so use the index on the list.

Van Sanders
Van Sanders
16,087 Points

Ahh, thats the push I needed. With your guys' help I completed this right away. Thanks a ton!