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 trial

Python Python Collections (Retired) Tuples Combo

Rabih Atallah
Rabih Atallah
3,405 Points

Please 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

zippy.py
# 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
Jose Soto
23,407 Points

This 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
Rabih Atallah
Rabih Atallah
3,405 Points

Thank Jose for your help! I appreciate it

Well the simplest way would be to use the zip function but that may negate the point of the exercise.

https://docs.python.org/3/library/functions.html#zip

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

You should a) not use zip in the exercise ;) and b) link to Python 3 docs instead of Python 2.

Good 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.