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

Gunhoo Yoon
Gunhoo Yoon
5,027 Points

How would you guys solve this?

I've been little fascinated by what Python can do even with the slight introduction to Python. I want to explore more possibilities to solve particular question but my thoughts are pretty limited. So I want to ask you guys, how would you solve this problem? I'd love to see Pythonic way of solving this problem.

These are what I came up with so far.

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.

#Feels very primitive, too verbose and hard to read.
def combo(iter_1, iter_2):
  result = []
  for i in range(len(iter_1)):
    result.append((iter_1[i], iter_2[i]))
  return result


#Might be optimal but I don't know
def combo(iter_1, iter_2):
  return list(zip(iter_1, iter_2))

1 Answer

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

combo() is meant to actually be zip() but without using zip(). Yes, I'm asking you to recreate a builtin.

I wouldn't use range() but I would use enumerate(). Then you only have to fetch one item by index.

Gunhoo Yoon
Gunhoo Yoon
5,027 Points

Great thanks Kenneth I will try that. Didn't even thought of that after watching video.

Gunhoo Yoon
Gunhoo Yoon
5,027 Points

I tried it and came up with this. Would this be what you meant by fetching one item by index? or am I off track?

combo.py
def combo(iter_1, iter_2):
  result = []

  for index, value in enumerate(iter_1):
    result.append((value, iter_2[index]))

  return result
Kenneth Love
Kenneth Love
Treehouse Guest Teacher

That's it. Seems cleaner to me.

Gunhoo Yoon
Gunhoo Yoon
5,027 Points

Cool! thanks for your support.