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 trialJin Sim
219 Points'combo' challenge - please explain to me how for...in... statement works.
Challenge Question: (https://teamtreehouse.com/library/python-collections-2/tuples/combo)
# combo([1, 2, 3], 'abc')
# Output:
# [(1, 'a'), (2, 'b'), (3, 'c')]
#Correct Answer:
def combo(it1, it2):
final = []
count = 0
for i in it1:
# <OR this also works:> for i in it2:
component = (it1[int(count)],it2[int(count)])
count += 1
final.append(component)
return final
#But why doesn't the one below work? This one makes more sense to me
#because you are iterating through both items in the 'combo' function, right?
#I may be misunderstanding something here so I would appreciate if someone can enlighten me!
def combo(it1, it2):
final = []
count = 0
for i in it1,it2:
component = (it1[int(count)],it2[int(count)])
count += 1
final.append(component)
return final
I thought I had a pretty good grasp of for...in... statements but apparently not!
I would appreciate any sort of explanation or comment that would help me better understand this.
Thanks!
1 Answer
Kaisar Turysbek
9,480 PointsI think your mistakes is that you use ' for i in it1, it2'. You need to use it like this: 'for i in it1'