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

Gavin Ralston
Gavin Ralston
28,770 Points

Can this implementation of combo() be the worst possible working example?

def combo(iter1, iter2):
  thelist = []
  for thing in enumerate(iter1):
    thelist.append(([thing[1], iter2[thing[0]]]))
  return thelist

It's pretty early for a New Years morning and I'm just not thinking clearly.

There's clearly a better way to do this, I just can't make my brain work. Seems I'm doing a lot of unnecessary things and there's a cleaner way to get the two iterables synced so I can zip the values together. That's a WHOLE lot of ugly brackets/parentheses at the end for a simple function.

...and I'm assuming list.append() isn't the only way I could do this, so I don't have to do the funky explicit () around list brackets to create my tuples

Maybe I should check this again after a few glasses of water and another nap...

1 Answer

boog690
boog690
8,987 Points
# 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(iter1, iter2):
  tup_list = []
  for index, value in enumerate(iter1):
    tup_list.append((value, iter2[index]))
  return tup_list

Here's my "less brackety" implementation. Same idea as yours, though.

Gavin Ralston
Gavin Ralston
28,770 Points

Yeah I was having an awful time with multiple assignment.... just need to not be coding today I think. I shouldn't have had that kind of trouble implementing a for..in loop

Thanks for the reply!