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

Travis Bailey
Travis Bailey
13,675 Points

Way off on zippy.py, but not sure exactly how to work this

With the lack of questions around this challenge I'm sure this is simple and I'm just not grasping something basic with Tuples. I saw the hint in the challenge about passing a new set of tuples when using .append, but I'm not quite sure what that means.

Am I trying to cheat by working with the function data as a list and just changing it to a tuple at the end? I've been trying for a couple days to get past this point and just can't get any further.

def combo(item_1, item_2):
  list_1 = []
  for items in item_1, item_2:
    count = 0
    list_1.append(item_1[count])
    list_2.append(item_2[count])
    count += 1
  return tuple(list_1)

1 Answer

Chris Shaw
Chris Shaw
26,676 Points

Hi Travis,

I would say it's a different way of going about it but not necessarily cheating, in the challenge itself we're just assuming that both lists are the same length so we can simply iterate over the first list and then use a counter to access the second.

def combo(items_1, items_2):
  tuples = []
  count = 0

  for items in items_1:
    tuples.append(tuple([items_1[count], items_2[count]]))
    count += 1

  return tuples

If you remember from the video tuples can be created using just a list with 2 or more values in it, a comma separated value or even a value with parentheses around it, above I've taking the list approach to simply cut down on code but you could also do the following.

def combo(items_1, items_2):
  tuples = []
  count = 0

  for items in items_1:
    my_tuple = items_1[count], items_2[count]
    tuples.append(my_tuple)
    count += 1

  return tuples

Again I think it comes down to personal preference, I personally like using a list and calling the tuple function as it provides more visibility on what's happening but that's just me.

Hope that helps.

Travis Bailey
Travis Bailey
13,675 Points

Thanks Chris. I think I got confused around how scope works within the function and how to express tuples in the function correctly. My first couple attempts I kept trying to get the for loop to reference both items_1 and items_2 which I figured was impossible. I kept thinking if I didn't mention the variable in the for loop like I did when defining the function I wouldn't be able to call it. You explanation helped a ton.