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

Nelson Chuang
Nelson Chuang
1,221 Points

Didn't get the right output. For example, expected (10, 'T') as the first item, got (10, 'T') instead.

looks right but error message seems strange

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(x,y):
  mylist=[]
  for a in x:
    for b in y:
      mylist.append((a,b))

  return mylist
Nelson Chuang
Nelson Chuang
1,221 Points

actually realized my two for loops should not be nested. I don't get how to do this one with what was taught.

2 Answers

Jennifer points out the issue. For each a in x you will get b tuples, so it looks like your total number of tuples will be a * b, a few more than expected.

Here's a second way to do it. One that uses enumerate():

def combo(iter1, iter2):
  combo_list = []
  for index, value in enumerate(iter1):
    tuple = value, iter2[index]
    combo_list.append(tuple)
  return combo_list
Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi, no those should not be nested. Odd though that you get that error from the challenge about the first element given that the first element is the only piece that will be correct after running your code. I'll show you how I did it:

def combo(x, y):
  myList = list()
  for z in range(len(x)):
    myList.append((x[z], y[z]))
  return myList

I took the liberty of editing your code to get to the final solution. In the instructions it explicitly states that you can assume that the length of the two inputs will be the same. So I looked over the range for the length of x. I could have just as easily looked at the length of y given that we know they're the same. Then I go through that list and append the value of x at the z index and y at the value of z index. Then we return the resulting list. Hope that helps!