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 trialGunhoo Yoon
5,027 PointsHow 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.
# 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
Treehouse Guest Teachercombo()
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
5,027 PointsGunhoo Yoon
5,027 PointsGreat thanks Kenneth I will try that. Didn't even thought of that after watching video.
Gunhoo Yoon
5,027 PointsGunhoo Yoon
5,027 PointsI tried it and came up with this. Would this be what you meant by fetching one item by index? or am I off track?
Kenneth Love
Treehouse Guest TeacherKenneth Love
Treehouse Guest TeacherThat's it. Seems cleaner to me.
Gunhoo Yoon
5,027 PointsGunhoo Yoon
5,027 PointsCool! thanks for your support.