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 trialRabih Atallah
3,405 PointsPlease is there a simpler script to solve this challenge
I feel my script is too long or complicated to return a list of tuples from L and S
# combo(['swallow', 'snake', 'parrot'], 'abc')
# Output:
# [('swallow', 'a'), ('snake', 'b'), ('parrot', 'c')]
# If you use list.append(), you'll want to pass it a tuple of new values.
# Using enumerate() here can save you a variable or two.
def combo(L,S):
X = []
B = []
i = 0
while i < len(L):
X.append(L[i])
X.append(S[i])
t = tuple(X)
B.append(t)
X.remove(L[i])
X.remove(S[i])
i = i + 1
return B
2 Answers
Jose Soto
23,407 PointsThis is a simpler method for the same result:
def combo(list, string):
result = []
for i, item in enumerate(list):
result.append((item, string[i]))
return result
Victor Rundbaken
14,037 PointsWell the simplest way would be to use the zip
function but that may negate the point of the exercise.
Kenneth Love
Treehouse Guest TeacherYou should a) not use zip
in the exercise ;) and b) link to Python 3 docs instead of Python 2.
Victor Rundbaken
14,037 PointsGood catch on the python 2 docs. And yep, zip kinda eliminates the purpose of the exercise. That's what I get for answering questions before waking up the brain :P
PS You look familiar. Wonder if I've seen you at a board game night or tech meetup in Portland.
Rabih Atallah
3,405 PointsRabih Atallah
3,405 PointsThank Jose for your help! I appreciate it