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 trialRakesh Bharadwaj
1,376 PointsCreate a function named combo that takes two ordered iterables. These could be tuples, lists, strings, whatever.
Create a function named combo that takes two ordered iterables. These could be tuples, lists, strings, whatever.
I could get the exact output as required
# combo([1, 2, 3], 'abc')
# Output:
# [(1, 'a'), (2, 'b'), (3, 'c')]
def combo(*args):
b =list(args)
#print(b)
d = list(b[0])
c = list(b[1])
e = ()
f = ()
i = 0
while i <= len(b):
e = (d[i],c[i])
f += (e,)
i += 1
return list(f)
3 Answers
Boris Ivan Barreto
6,838 PointsI managed to solve the task this way
def combo(*arg):
my_result = []
for index, val in enumerate(arg[0]):
my_result.append((val, arg[1][index]))
return my_result
Viktor Léhner
12,771 PointsVery nice and simple solution!
Steven Parker
231,236 PointsBoris's solution is clever, but rather unconventional and complicated. Much more concise solutions are possible, and it's also not necessary to use "enumerate" or the unpacking operator. For example:
def combo(a, b):
return [(a[i], b[i]) for i in range(len(a))]
Bruno Aldo Lunardi
15,795 PointsBruno Aldo Lunardi
15,795 PointsI had quite a tough time thinking about a shorter solution for this one and you provided such an elegant and simple solution here, thanks !!
Christian A. Castro
30,501 PointsChristian A. Castro
30,501 PointsBoris Ivan Barreto beautiful!! thanks for the hint