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

Jesse Miller
Jesse Miller
7,920 Points

Keeps returning double

I've tried everything to return just one value from the lst. For example, I keep getting [(1,a), (1,b), (1,c), (2,a), (2,b), (2,c), (3,a), (3,b), (3,c)] instead what the criteria wants. Anyone know how to fix this?

zippy.py
# combo([1, 2, 3], 'abc')
# Output:
# [(1, 'a'), (2, 'b'), (3, 'c')]
# If you use .append(), you'll want to pass it a tuple of new values.
def combo(lst, string):
  newlist=[]
  for i in lst:
    for k in string:
      newlist.append((i, k))
  return newlist

1 Answer

Steven Parker
Steven Parker
229,744 Points

You have two nested iterations, so you are paring each of the first items with every second list item.

What you want is a single iteration that will pair the first with the first, second with second, and so on.

Hint: use a counter to index both lists at the same time.

I'll bet you can do it now. But if you're still stuck (don't look below until you try!):


:warning: SPOILER ALERT


def combo(lst, string):
  newlist=[]
  for i in range(len(lst)):
    newlist.append((lst[i], string[i]))
  return newlist