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 trialAndrew Nguyen
3,032 PointsCombo strings problem
Hello. I'm having some difficulty solving this problem as if I switch what goes into my function it works. However, the problem set says anything can be pasted into the function. What I have does not work because my string cannot be interpreted as an integer, furthermore, I don't have any control on what could be passed. Pretty lost as to where to go from here. I've done some reading on 'zip' but I want to try and figure this out using enumerate.
# combo([1, 2, 3], 'abc')
# Output:
# [(1, 'a'), (2, 'b'), (3, 'c')]
def combo(item1, item2):
mylist = []
for x in enumerate(item1, start=item2[0]):
mylist.append(x)
print(mylist)
myList = [1, 2, 3]
myString = "abc"
combo(myList, myString)
2 Answers
Alexander Davison
65,469 PointsYou don't need to use enumerate
, it is unnecessary here.
To reduce complexity of your solution, use a regular for
loop that iterates through 0 to N-1
, where N
is len(item1)
(item1
and item2
are of the same length)
On every iteration, append a newly generated tuple to the accumulating list, then after the loop, return the list
def combo(iter1, iter2):
combo_list = []
for i in range(len(iter1)):
combo_list.append((iter1[i], iter2[i]))
return combo_list
Andrew Nguyen
3,032 PointsThanks Alexander. I was definitely over thinking the problem and my solution.
Alexander Davison
65,469 PointsNo problem :)
If this helped, don't forget to provide a Best Answer to signify that the question has been answered