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

Dhruv Ghulati
Dhruv Ghulati
1,582 Points

Not sure about how to get this right

I think I have the right idea for my for loop for creating tuple (1), tuple (2) etc. and then appending tuple(1), tuple (2) etc. to a larger list. Where am I going wrong?

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):
  tuples=list()
  for i in it1,it2,subtuple:
    subtuple(i)=(it1[i],it2[i])   
  for i in subtuple:
    tuples.append(subtuple(i))
  return tuples

Take another look at how you have defined your tuples variable ... you'll want to define it as a tuple. You should only need 1 loop to count over the animals, then use your list.append for each count, alphabetic. finally return the tuple.

Dhruv Ghulati
Dhruv Ghulati
1,582 Points

Are you sure? I thought the function needs to return a list of tuples, not a tuple of tuples?

I have amended and tried again with one loop. This keeps creating a new variable, my_tuple, and then appending that to tuplelist.

For some reason though, this is still not working...

def combo(it1,it2):
  tuplelist=list()
  for i in it1,it2:
    my_tuple=(it1[i],it2[i])
    tuplelist.append(my_tuple)
  return tuplelist

1 Answer

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

Dhruv, you don't need two for loops, because the question tells you that Assume the iterables will be the same length.

you can approach this problem with enumerate(), but if you don't want to use it (since I didn't see you use it in your code), here's another approach.

def combo(iter1, iter2):
  result = []
  for index in range(len(iter1)):
    # append the (iter1[index], iter2[index]) tuple to result list.
    result.append((iter1[index], iter2[index]))
  return result

I'm not sure if you have seen range() in action in lecture video yet. if not, then here's a short example:

for i in range(5):
  print(i)

# => 0
# => 1
# => 2
# => 3
# => 4